By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,684 Members | 1,439 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

XERCES and getNodeValue confussion

sandipan169
P: 2
A DOMElement may contain a collection of other DOMElements or a DOMText. To get the text value of an element you need to call the method getTextContent(), getNodeValue will always return NULL. The is another better way conceptually, as the DOMText is a child of the DOMElement we can traverse through the child node and get the value.

Below is the logic in the form of a method:

Expand|Select|Wrap|Line Numbers
  1. string getElementValue(const DOMElement& parent)
  2. {    
  3.  DOMNode *child;   
  4.  
  5.  string strVal;
  6.  
  7.  for (child = parent.getFirstChild();child != NULL ; child = child->getNextSibling())    
  8.  {    
  9.     if(DOMNode::TEXT_NODE == child->getNodeType())    
  10.     {    
  11.      DOMText* data = dynamic_cast<DOMText*>(child);    
  12.      const XMLCh* val = data->getWholeText();    
  13.      strVal += XMLString::transcode(val);    
  14.     }    
  15.     else    
  16.     {    
  17.         throw "ERROR : Non Text Node";    
  18.     }    
  19.  }    
  20.  return strVal;   
  21. }
Hope this helps :)

Follow me on : http://mycpplearningdiary.blogspot.com/
Jan 29 '12 #1
Share this Article
Share on Google+