如何解决xerces:如何获取带有特定标签的 xml 节点?
QDomElement tmp = root.firstChildElement("A");
QDomElement tt = tmp.firstChildElement("C");
使用具有此结构的 xml 文件:
<A>
<B></B>
<C></C>
</A>
使用 xerces,我该怎么做?
解决方法
xercesc_3_2::XMLPlatformUtils::Initialize();
// create the DOM parser
xercesc_3_2::XercesDOMParser* parser = new xercesc_3_2::XercesDOMParser;
parser->setValidationScheme(xercesc_3_2::XercesDOMParser::Val_Never);
parser->parse(fileName.c_str());
// get the DOM representation
xercesc_3_2::DOMDocument* doc = parser->getDocument();
// get the root element
xercesc_3_2::DOMElement* root = doc->getDocumentElement();
// evaluate the xpath
xercesc_3_2::DOMXPathResult* result = doc->evaluate(
xercesc_3_2::XMLString::transcode("/doc/document/childL1/childL2/Node_to_change"),// "A/B" in the provided xml sample for change B text node value
root,NULL,xercesc_3_2::DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,NULL);
并更改值:
if (result->getNodeValue() == NULL)
{
cout << "There is no result for the provided XPath " << endl;
}
else
{
result->getNodeValue()->getFirstChild()->setNodeValue(xercesc_3_2::XMLString::transcode("3000"));
const XMLCh* a = result->getNodeValue()->getFirstChild()->getNodeValue();
char* tttt = (char*)a;
cout << "Node value: " << tttt << endl;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。