开发者

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>
                    <开发者_如何学Go/CAT>
                </GRP>
            </Commands>
        </Destination>
    </Context>
</GET>

if possible give an example code.


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜