Reputation: 93
I wrote a XML parser using the Xerces C++ API. I have method for getting node values that seems to work intermittently and I am not sure why.
I am new to XML so pardon me if I dont have all the lingo correct.
For example, I can successfully validate parse an XML file like the following:
<?xml version="1.0" encoding="UTF-8"?>
<RequestMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../schema/Config.xsd">
<MsgHeader>
<MessageID>0</MessageID>
<Gic>0000</Gic>
<Fcg>1</Fgc>
<EventID>0</EventID>
</MsgHeader>
<PrimaryRate>
<Rate>MAX_RATE</Rate>
<Value>1</Value>
</PrimaryRate>
<SecondaryRate>
<Rate>MAX_RATE</Rate>
<Value>2</Value>
</SecondaryRate>
<Mode>Enable</Mode>
<Toggle>On</Toggle>
</RequestMessage>
Let say for example I am looking for the value of "Fgc" under "MsgHeader". I can successfully get the node name of message header using DOMNode::getNodeName(), and I can get all of the child nodes in a DOMNodeList and loop through them. But as I am looping through the child nodes and printing out their node names using DOMNode::getNodeName(), the string #Text is printed. When trying to get the value using DOMNode::getNodeValue() or DOMNode::getTextContent(), the string is empty.
For example:
xercesc::DOMNodeList *list = DOMDoc->getElementsByTagName(tagname);
for(XMLSize_t i=0; i<list->getLength(); i++) {
if(list->item(i)->hasChildNode()) {
xercesc::DOMNodeList *children = nodeList->item(i)->getChildNodes();
for(XMLSize_t j=0; j<list->getLength(); j++) {
xercesc::DOMNode *node = list->item(j);
XMLCh *name = node->getNodeName();
XMLCh *value = node->getNodeValue();
XMLCh *text = node->getTextContent();
cout << "Name: " << xercesc::XMLString::Transcode(name) << endl;
cout << "Value: " << xercesc::XMLString::Transcode(value) << endl;
cout << "Text: " << xercesc::XMLString::Transcode(text) << endl;
}
}
}
OUTPUT:
Name: #Text
Value:
Text:
Any insight would be greatly appreciated!
Upvotes: 3
Views: 1974
Reputation: 335
By default, xerces considers white spaces (tabs, end lines and spaces) as textNodes. But you can set the following option to your parser (inherited from AbstractDOMParser):
domParser.setIncludeIgnorableWhitespace(false);
And the white spaces are ignored during parsing.
Upvotes: 3