Hemanth D. wrote:
I have the following code snippet to traverse a NodeList (all element
nodes retrieved using getElementsByTagName). The getNodeName() retrieves
the correct name. However, the getNodeValue() is not printing out the
value. (We have tried dynamic_cast of xNode3 to a DOMElement also but
this also doesn't work. We have also tried using getFirstChild()/
getNextSibling() instead of the below for-loop - but same result. The
NodeName returns the correct value but the NodeValue doesn't)
Can someone please help identifying the problem?
You need to read a good tutorial on the DOM, so you understand how it works.
The parser used is XercesDOMParser - parsing a XML using a
MemBufInputSource (rather than a XML file)
DOMNodeList* xNodeList3 = xNode->getChildNodes();
const XMLSize_t ixNodeCount3 = xNodeList3->getLength();
for (int i3=0; i3<ixNodeCount3; i3++)
{
DOMNode* xNode3 = xNodeList3->item(i3);
if (DOMNode::ELEMENT_NODE == xNode3->getNodeType())
{
char* sxNode3Name =
XMLString::transcode(xNode3->getNodeName()); // OK
char* sxNode3Value =
XMLString::transcode(xNode3->getNodeValue()); //
retrieves null string
DOMElemement::getNodeValue() will always return an empty string, because
the "value" of an element node is in its child(ren). In your case, the
text node child.
You can either iterate through the children, or call
DOMNode::getTextContent(). My recommendation would be to iterate through
the children, as getTextContext() will allocate memory that you cannot free
without destroying the document.
Dave