minu
minu

Reputation: 157

how to get node value using xerces c++

Is it possible with xerces-c++ library getting only value of Destination Node from following XML string or file?

<GET>
    <Context>
        <Destination>DATA 
            <Commands>
                <GRP>VAL
                    <CAT>SET 
                        <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
                            <title>The Autobiography of Benjamin Franklin</title>
                            <author>
                              <first-name>Benjamin</first-name>
                              <last-name>Franklin</last-name>
                            </author>
                            <price>8.99</price>
                        </book>
                    </CAT>
                </GRP>
            </Commands>
        </Destination>
    </Context>
</GET>

if possible give an example code.

Upvotes: 3

Views: 7873

Answers (1)

Sandipan Karmakar
Sandipan Karmakar

Reputation: 41

You can achieve this using XPath in Xalan C++ library. But only using Xerces C++ lib you need to do it the hard way

Below is the logic in the form of a method:

string getDestinationValue(const DOMDocument& xmlDoc) 
{
DOMElement* elementRoot = xmlDoc->getDocumentElement();
DOMNode *child = elementRoot->getFirstChild()->getFirstChild()->getFirstChild();
string strVal;
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 :)

Sandipan Karmakar

Follow me on : http://mycpplearningdiary.blogspot.com/

Upvotes: 3

Related Questions