2

I'm looking for a simple way to read XML that is contained in a string variable in C++ for Unix systems. I have found a lot of solutions for xml files but not a lot about XML that is contained in a string variable.

Do I have to use functions such as find(), etc. or is there a library that I missed that could do the job for me ?


EDIT:

If I have the following XML in a string, how can I read it ? (with pugixml for example)

string xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
     xml += "<people><person><name>Toto</name><age>20</age></person>";
     xml += "<person><name>John</name><age>42</age></person></people>";
Aleph0
  • 470
  • 2
  • 10
  • 27
  • Do you want to read the xml in C++ or on the unix command line? – Leon Nov 27 '14 at 07:34
  • @Leon In C++ but I don't want a solution for Windows. ;) – Aleph0 Nov 27 '14 at 07:36
  • I can't give a definitive answer as I have never had to parse xml in c++, but from experience using lxml in python I think you should have a look at libxml2 – Leon Nov 27 '14 at 07:40
  • 1
    have a look at [What XML parser should I use in C++?](http://stackoverflow.com/q/9387610) and [Best open XML parser for C++](http://stackoverflow.com/q/170686) – AliciaBytes Nov 27 '14 at 07:44
  • If you don't need it to be super-duper function complete but you want it to be fast and easy to use, I recommend [pugixml](http://pugixml.org/). – Galik Nov 27 '14 at 07:50
  • I added an example of a string that contains XML in my question. Can someone give me a code sample to parse it ? – Aleph0 Nov 27 '14 at 09:13
  • @Aleph0, the resulting xml is invalid (unless you remove the duplicated line). Either way, [the pugixml documentation](http://pugixml.googlecode.com/svn/tags/latest/docs/manual/loading.html) will give you an example (search for "Loading document from memory" within the page). – utnapistim Nov 27 '14 at 09:45
  • ***Every*** mature XML library will support loading of strings... just pick one and read the docs/tutorials. – Tony Delroy Nov 27 '14 at 10:30

2 Answers2

3

I would recommend to use Tinyxml2 library. Especially XMLDocument::Parse - is your best catch (http://grinninglizard.com/tinyxml2docs/classtinyxml2_1_1_x_m_l_document.html#a1819bd34f540a7304c105a6232d25a1f)

And after loading your string in XMLDocument - you can navigate it via tinyxml2 tools further.

Exact example in documentation -> http://www.grinninglizard.com/tinyxml2docs/_example-3.html

Starl1ght
  • 4,422
  • 1
  • 21
  • 49
  • Thanks, it is exactly what I was looking for. I just had to replace XMLDocument by TiXmlDocument and XMLElement by TiXmlElement in the example. – Aleph0 Nov 27 '14 at 13:09
3

Here is an example using pugixml:

#include <string>
#include <iostream>

#include "pugixml.hpp"

int main()
{
    using namespace pugi;

    std::string xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
    xml += "<people><person><name>Toto</name><age>20</age></person>";
    xml += "<person><name>John</name><age>42</age></person></people>";

    xml_document doc;
    xml_parse_result res = doc.load(xml.c_str());

    if(!res)
    {
        std::cout << "ERROR: " << res.description() << '\n';
        return 1;
    }

    for(auto&& person: doc.child("people").children("person"))
    {
        std::cout << "name: " << person.child("name").text().as_string() << '\n';
        std::cout << " age: " << person.child("age").text().as_string() << '\n';
        std::cout << '\n';
    }

}

Output:

name: Toto
 age: 20

name: John
 age: 42
Galik
  • 47,303
  • 4
  • 80
  • 117
  • I managed to make it work with tinyxml and it looks more or less the same. Too bad I can't validate 2 answers but thank you for this answer. ;) – Aleph0 Nov 27 '14 at 15:27