[英]How to retrieve node and particular element string from xml file in C++ using libxml2 by using xpath?
How to retrieve text value in c++ using libxml? 如何使用libxml在C ++中检索文本值?
Here is the XML: 这是XML:
<?xml version="1.0" encoding="UTF-8"?>
<Help xmlns="http://www.example.org/HelpFileStructure" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/HelpFileStructure HelpFileStructure.xsd ">
<Text>dfgdfg</Text>
</Help>
Code: 码:
void Help::HelpName()
{
string Help_text;
parser.parse_file(XmlFileName);
Node* root = parser.get_document()->get_root_node();
NodeSet result = root->find("/Help/Text");
Element *first_element = (Element *)result.at(0);
Help_text = first_element->get_child_text()->get_content();
}
Use this: 用这个:
NodeSet result = root->find("//*[local-name()='Help']/*[local-name()='Text']");
That gets a node set where each node is an element named Text
, regardless of what namespace the element is in, that's a child of any element named Help
, regardless of what namespace it's in. 这样就得到了一个节点集,其中每个节点都是一个名为Text
的元素,无论该元素位于哪个命名空间中,它都是任何名为Help
元素的子元素,无论它位于哪个命名空间中。
If you only want Help
and Text
elements in the http://www.example.org/HelpFileStructure
namespace, and want to ignore any Text
and Help
elements the document might have that are in some other namespace, then you can use the namespace-uri()
function like this: 如果您只想在http://www.example.org/HelpFileStructure
命名空间中使用Help
和Text
元素,并且想忽略文档可能在其他命名空间中包含的任何Text
和Help
元素,则可以使用namespace-uri()
函数如下:
NodeSet result = root->find("//*[local-name()='Help' and namespace-uri()='http://www.example.org/HelpFileStructure']/*[local-name()='Text' and namespace-uri()='http://www.example.org/HelpFileStructure']");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.