Search code examples
c++pugixml

pugixml generates no output


I try to read an .xml file to my C++ program using pugixml. The problem is, that not even the first example works.

My .xml looks like this:

<?xml version='1.0' encoding='UTF-8'?>
 <trk>
    <name bahn="Bahn 1"/>
    <trkseg>
      <trkpt lat="48.0815471" lon="11.2745363">
      </trkpt>
      <trkpt lat="48.0815471" lon="11.2745363">
      </trkpt>
    </trkseg>
  </trk>
  <trk>
    <name bahn ="Bahn 2"/>
    <trkseg>
      <trkpt lat="48.08161089580602" lon="11.274945462810345">
      </trkpt>
      <trkpt lat="48.08161089580602" lon="11.274945462810345">
      </trkpt>
    </trkseg>
  </trk>

and I try to load the document using

pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file("kreisel_argelsriederfeld.xml");
std::cout<<"Load result: " << result.description()<<", mesh name"<< doc.child("name").attribute("bahn").value()<<std::endl;


pugi::xml_node bahnen = doc.child("trkseg");
std::cout << bahnen.value()<<std::endl;

Though when I call it, the output gets to ", mesh name" and afterwards shows only empty space. Where is my mistake? Is it because I call the wrong names or something like that? As "Load result" I get "No Error", so the file should be loaded, doesn't it? I found several examples and tutorials, but all of them use this to load the file.


Solution

  • The file isn't valid XML (only one top-level <trk> element is allowed). See this pugixml error handling example.

    If you want to have more than one <trk> they need to be wrapped in a single top level <trks> element.

    Edit: Missed the part about there being no error. Perhaps pugixml is being tolerant here and the real problem is that <trk> is considered a child of the document node. So, instead of doc.child("name") try doc.child("trk").child("name").