Overview
Features
Download
Documentation
Community
Add-Ons & Services

XML : attribute node name

A general discussion forum.

XML : attribute node name

Postby cdelmas » 27 Sep 2006, 12:59

Hello all,
I have a problem while using XML DOM parser. When I try to print the DOM tree, I cannot print the attributes (nodeName returns "", and hasAttribute returns false). I think it is because of a bug. I have a source code here, so if anybody could tell me whether it's a bug or not :

//first method
void printTree(Poco::XML::NodeList* nodeList, unsigned int nbTabs = 0) {
unsigned long childrenCount = nodeList->length();
for(unsigned long i = 0 ; i < childrenCount ; ++i) {
Poco::XML::Node* node = nodeList->item(i);
if(node->hasChildNodes()) { // parent node
formatted(node->nodeName(), nbTabs); // print the node name
if(node->hasAttributes()) {
Poco::XML::NamedNodeMap* map = node->attributes();
// prints the attributes
unsigned long attrCount = map->length();
for(unsigned long j = 0 ; j < attrCount ; ++j) {
Poco::XML::Node* attr = map->item(j);
Poco::XML::XMLString qname = attr->nodeName(); //=> empty (bug ?) [#1]
Poco::XML::XMLString name = attr->localName(); // ok
Poco::XML::XMLString value = attr->getNodeValue();
Poco::XML::XMLString text = name + Poco::XML::XMLString(" = ") + value;
formatted(text,nbTabs,false);
}
}
printTree(node->childNodes(),nbTabs+1);
} else { // leaf
formatted(node->getNodeValue(),nbTabs);
}
}
}

// second method
void iterateThrough(Poco::XML::Node* root) {
Poco::XML::NodeIterator iterator(root,Poco::XML::NodeFilter::SHOW_ALL);
Poco::XML::Node* current = 0;
while((current = iterator.nextNode()) != 0){
formatted(current->nodeName(),1);
Poco::XML::Element* e = dynamic_cast(current);
if(e != 0 && e->hasAttribute(Poco::XML::XMLString("brief"))) { // doesn't work because of #1
Poco::XML::XMLString attr = e->getAttribute(Poco::XML::XMLString("brief"));
}
}
}

Thanks a lot.
cdelmas
 
Posts: 46
Joined: 22 Sep 2006, 10:04
Location: France

Re: XML : attribute node name

Postby guenter » 27 Sep 2006, 14:03

Before parsing the XML file with the DOMParser, do the following:

domParser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, true);

and getNodeName will work.

As per default, the underlying SAX parser does not report a qualified name, thus the node name is empty. Enabling FEATURE_NAMESPACE_PREFIXES fixes that.
guenter
 
Posts: 1119
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: XML : attribute node name

Postby cdelmas » 28 Sep 2006, 10:20

I tried your solution, but it doesn't work. The result is the same. In order to understand, I tried :
Poco::XML::Element* element = doc->getElementById(Poco::XML::XMLString("bk100"));
result : element is null.
cdelmas
 
Posts: 46
Joined: 22 Sep 2006, 10:04
Location: France

Re: Re: XML : attribute node name

Postby cdelmas » 29 Sep 2006, 11:02

> Before parsing the XML file with the DOMParser, do the following:
>
> domParser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, true);
>
> and getNodeName will work.
>
> As per default, the underlying SAX parser does not report a qualified name, thus the node name is empty. Enabling FEATURE_NAMESPACE_PREFIXES fixes that.

It appears that getElementById doesn't work. I tried this solution, but it still returns a nul pointer. attr->nodeName() returns "" again, and e->hasAttribute returns false. I think there is a problem with attributes (maybe qualified names are not registered ?), but I can't put the finger on it.
cdelmas
 
Posts: 46
Joined: 22 Sep 2006, 10:04
Location: France

Re: Re: Re: XML : attribute node name

Postby guenter » 29 Sep 2006, 16:22

Try the latest patch from http://sourceforge.net/tracker/index.php?func=detail&aid=1567051&group_id=132964&atid=725709

As for getElementById(), its documentation explains why it does not work. Try the upcoming 1.2.4 release (soon available from the SVN repository's trunk; available Monday for download), which contains a two-argument getElementById() and getElementByIdNS(). This might be what you're looking for.

guenter
 
Posts: 1119
Joined: 11 Jul 2006, 16:27
Location: Austria


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 7 guests