【问题标题】:how to get node value using xerces c++如何使用 xerces c++ 获取节点值
【发布时间】:2011-11-09 14:49:51
【问题描述】:

xerces-c++ 库是否可以从以下 XML 字符串或文件中仅获取目标节点的值?

<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>

如果可能的话,给出一个示例代码。

【问题讨论】:

  • 你期望那个节点的值是多少?

标签: xml windows-xp xerces


【解决方案1】:

您可以使用 Xalan C++ 库中的 XPath 实现此目的。但只有使用 Xerces C++ 库,你才需要努力

下面是方法形式的逻辑:

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;
}

希望这会有所帮助:)

桑迪潘·卡玛卡

关注我:http://mycpplearningdiary.blogspot.com/

【讨论】:

    猜你喜欢
    • 2020-08-11
    • 2021-04-19
    • 2019-12-03
    • 2021-05-14
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    相关资源
    最近更新 更多