[英]How to retrieve node and particular element string from xml file in C++ using libxml2 without using xpath?
[英]How to retrieve node and particular element string from xml file in C++ using libxml2 by using xpath?
如何使用libxml在C ++中檢索文本值?
這是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>
碼:
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();
}
用這個:
NodeSet result = root->find("//*[local-name()='Help']/*[local-name()='Text']");
這樣就得到了一個節點集,其中每個節點都是一個名為Text
的元素,無論該元素位於哪個命名空間中,它都是任何名為Help
元素的子元素,無論它位於哪個命名空間中。
如果您只想在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.