1

I'm trying to parse an XML file using the boost/propert_tree library. I can get the xml file correctly and everything, but when I look for childs, it doesn't find any.

I have an input.xml file:

<ax:hello someatribute:ax="dwadawfesfjsefs">
    <something>523523</something>
    <ax:whatever>
        <ax:service_tree>
            <ax:service>some</ax:service>
            <ax:url>someulr</ax:url>
        </ax:service_tree>
    </ax:whatever>
</ax:hello>

The function where I try to parse the xml:

void parseXml(std::istream &stream)
{
    using boost::property_tree::ptree;
    ptree pt;
    read_xml(stream, pt);
    BOOST_FOREACH(ptree::value_type const &value, pt.get_child("ax:hello"))
    {
        std::cout << value.first;
    }
}

And the main function:

int main()
{
    std::ifstream stream("input.xml");
    parseXml(stream);
    return 0;
}

The error message I get is:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl >' what(): No such node (ax:hello) Aborted (core dumped)`

As you can see, the ax:hello tag is properly opened and closed, so it should be able to find it despite the attributes, right?

Hope someone knows what's going on in here!

lpares12
  • 3,504
  • 4
  • 24
  • 45

1 Answers1

2

You're doing something else wrong/different:

Live On Coliru

#include <iostream>
#include <fstream>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>

void parseXml(std::istream &stream)
{
    using boost::property_tree::ptree;
    ptree pt;
    read_xml(stream, pt);
    BOOST_FOREACH(ptree::value_type const &value, pt.get_child("ax:hello"))
    {
        std::cout << value.first << "\n";
    }
}

int main()
{
    std::istringstream stream(R"(
        <ax:hello someatribute:ax="dwadawfesfjsefs">
            <something>523523</something>
            <ax:whatever>
                <ax:service_tree>
                    <ax:service>some</ax:service>
                    <ax:url>someulr</ax:url>
                </ax:service_tree>
            </ax:whatever>
        </ax:hello>
)");
    parseXml(stream);
}

Prints

<xmlattr>
something
ax:whatever

Somewhat more elaborate dumping:

void dump(ptree const& pt, std::string const& indent = "") {
    for (auto& node : pt) {
        std::cout << indent << node.first;
        auto value = boost::trim_copy(node.second.get_value(""));
        if (!value.empty())
            std::cout << ": '" << value << "'";
        std::cout << "\n";
        dump(node.second, indent + "    ");
    }
}

Prints Live On Coliru too

ax:hello
    <xmlattr>
        someatribute:ax: 'dwadawfesfjsefs'
    something: '523523'
    ax:whatever
        ax:service_tree
            ax:service: 'some'
            ax:url: 'someulr'
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Do you know why it doesn't work when passing an istream with a file instead of a istringstream? Could you explain what `auto` does in the `dump` function? Which type would it be? – lpares12 Jul 24 '16 at 15:29
  • There's no difference. Obviously i used you code + file locally. The dump function is just for demo and uses c++11 – sehe Jul 24 '16 at 15:31
  • Yes there is, or at least I'm doing it wrong, that's why I posted the question. It doesn't find any nodes when I do it from a file. – lpares12 Jul 24 '16 at 15:50
  • Yes it does. I showed you exactly that. If you don't want to hear it, fine. Look for the other differences. Wrong file, wrong path, wrong encoding, you know. Debug it. – sehe Jul 24 '16 at 17:58
  • I did, that's why I asked the question here. Your code saves the contents of input.xml directly inside a istringstream, instead of loading the file into a ifstream. Still thanks for the answer because I was able to keep going. – lpares12 Jul 24 '16 at 18:02
  • I don't know what to say. [There is no difference](http://coliru.stacked-crooked.com/a/5c0fd4e8c177661f). Like I told you several times. I don't care what you choose to believe. Good luck. ([bonus](http://coliru.stacked-crooked.com/a/262954ad1d3e828e)) – sehe Jul 24 '16 at 19:18
  • Like I told you, there is a big difference between copy-pasting a file into the code and loading a file to the program at runtime. Your code is correct? Yes. Does it work as I want? No. Why? because instead of loading the file with ifstream it's a simple copy-paste of the contents into a variable. – lpares12 Jul 24 '16 at 19:23
  • I have come to the conclusion you must be blind. – sehe Jul 24 '16 at 19:25