Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xerces DOMNode returns node name as #Text

Tags:

c++

xml

xerces

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!

like image 835
user2884074 Avatar asked Sep 16 '26 13:09

user2884074


1 Answers

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.

like image 83
Jerome Demantke Avatar answered Sep 19 '26 06:09

Jerome Demantke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!