1

I am using xerces c++ to manipulate an xml file? but getNodeValue() and setNodeValue() are not working but getNodeName() is working. Do anyone has any suggestions?

 if( currentNode->getNodeType() &&  currentNode->getNodeType() == DOMNode::ELEMENT_NODE ) 
         {
        // Found node which is an Element. Re-cast node as element
            DOMElement* currentElement= dynamic_cast< xercesc::DOMElement* >( currentNode );
            if( XMLString::equals(currentElement->getTagName(), TAG_ApplicationSettings))
            {
               // Already tested node as type element and of name "ApplicationSettings".
               // Read attributes of element "ApplicationSettings".
               const XMLCh* xmlch_OptionA = currentElement->getAttribute(ATTR_OptionA);
               m_OptionA = XMLString::transcode(xmlch_OptionA);
                   XMLCh* t,*s;
        //s= XMLString::transcode("manish");
        //currentNode->setElementText(s);
                  t=(XMLCh*)currentNode->getNodeName();
               s=(XMLCh*)currentNode->getNodeValue();

cout<getNodeValue()) << "\n";

2
  • there is no error its just not working Commented Apr 4, 2011 at 11:45
  • so what is the node you're parsing, what are expecting, what are you getting ? Commented Apr 4, 2011 at 14:13

2 Answers 2

1

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:

string getElementValue(const DOMElement& parent)
{    
 DOMNode *child;   

 string strVal;

 for (child = parent.getFirstChild();child != NULL ; child = child->getNextSibling())    
 {    
    if(DOMNode::TEXT_NODE == child->getNodeType())    
    {    
     DOMText* data = dynamic_cast<DOMText*>(child);    
     const XMLCh* val = data->getWholeText();    
     strVal += XMLString::transcode(val);    
    }    
    else    
    {    
        throw "ERROR : Non Text Node";    
    }    
 }    
 return strVal;   
}

Hope this helps :)

Sign up to request clarification or add additional context in comments.

Comments

1

getNodeValue() will always return an empty string, because the "value" of an element node is in its child. In our case it is text node child. Either way is to iterate through child nodes or use getTextContent. First check for child nodes in a node using hasChildNodes() then use methods like getFirstChild() etc. . Afterwards use getNodeValue().

DOMNode* ptrDomNode = SomeNode;

if(ptrDomNode->hasChildNodes())
{
    DOMNode* dTextNode =  ptrDomNode->getFirstChild();         
    char* string = XMLString::transcode(dTextNode->getNodeValue());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.