user3214224 user3214224 - 4 months ago 23
C++ Question

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?

Here is the XML:

<?xml version="1.0" encoding="UTF-8"?>
<Help xmlns="" xmlns:xsi="" xsi:schemaLocation=" HelpFileStructure.xsd ">


void Help::HelpName()
string Help_text;
Node* root = parser.get_document()->get_root_node();
NodeSet result = root->find("/Help/Text");
Element *first_element = (Element *);
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.

If you only want Help and Text elements in the namespace, and want to ignore any Text and Help elements the document might have that are in some other namespace, then you can do use the namespace-uri() function like this:

NodeSet result = root->find("//*[local-name()='Help' and namespace-uri()='']/*[local-name()='Text' and namespace-uri()='']");