I need a way to extract the value between the tags, given a path in the XML. How can I use TinyXpath for this?

230 Views Asked by At

XML EXMAPLE:

<first node>

   <second node>
           hello 
   </second node>

   <third node>
           abcd
    </third node>
</first node>

If the Xml file looks like above and I give an input path "first node/second node", I must be able to get the result as "hello".

If the input path is "first node/third node", the result should be "abcd"

1

There are 1 best solutions below

0
On

You have invalid XML: tag names cannot contain spaces and end tags are written like </tag>, not <tag/>.

Here, I fixed XML for you:

<first-node>
   <second-node>
           hello 
   </second-node>

   <third-node>
           abcd
   </third-node>
</first-node>

Now to get second-node and third-node contents you need proper XPath expression, e.g. //first-node/second-node/text() and //first-node/third-node/text().

Here is complete example:

TiXmlDocument doc;
if (doc.LoadFile("example.xml"))
{
   // Will be "hello"
   TIXML_STRING s1 = TinyXPath::S_xpath_string(
       doc.RootElement(),
       "//first-node/second-node/text()");

   // Will be "abcd"
   TIXML_STRING s2 = TinyXPath::S_xpath_string(
       doc.RootElement(),
       "//first-node/third-node/text()");
}