Search code examples
c++xml-parsingpugixml

PugiXML C++ getting content of an element (or a tag)


Well I'm using PugiXML in C++ using Visual Studio 2010 to get the content of an element, but the thing is that it stops to getting the value when it sees a "<" so it doesn't get the value, it just gets the content till it reaches a "<" character even if the "<" is not closing its element. I want it to get till it reaches its closing tag even if it ignores the tags, but only the text inside of the inner tags, at least.

And I also would like to know how to get the Outer XML for example if I fetch the element

pugi::xpath_node_set tools = doc.select_nodes("/mesh/bounds/b"); what do I do to get the whole content which would be " Link Till here"

this content is the same given down here:

#include "pugixml.hpp"

#include <iostream>
#include <conio.h>
#include <stdio.h>

using namespace std;

int main//21
    () {
    string source = "<mesh name='sphere'><bounds><b id='hey'> <a DeriveCaptionFrom='lastparam' name='testx' href='http://www.google.com'>Link Till here<b>it will stop here and ignore the rest</b> text</a></b> 0 1 1</bounds></mesh>";

    int from_string;
    from_string = 1;

    pugi::xml_document doc;
    pugi::xml_parse_result result;
    string filename = "xgconsole.xml";
    result = doc.load_buffer(source.c_str(), source.size());
    /* result = doc.load_file(filename.c_str());
    if(!result){
        cout << "File " << filename.c_str() << " couldn't be found" << endl;
        _getch();
        return 0;
    } */

        pugi::xpath_node_set tools = doc.select_nodes("/mesh/bounds/b/a[@href='http://www.google.com' and @DeriveCaptionFrom='lastparam']");

        for (pugi::xpath_node_set::const_iterator it = tools.begin(); it != tools.end(); ++it) {
            pugi::xpath_node node = *it;
            std::cout << "Attribute Href: " << node.node().attribute("href").value() << endl;
            std::cout << "Value: " << node.node().child_value() << endl;
            std::cout << "Name: " << node.node().name() << endl;

        }

    _getch();
    return 0;
}

here is the output:

Attribute Href: http://www.google.com
Value: Link Till here
Name: a

I hope I was clear enough, Thanks in advance


Solution

  • That's how XML works. You can't embed < or > right in your values. Escape them (e.g. using HTML entities like &lt; and &gt;) or define a CDATA section.