0

I'm trying to parse a string containing an XML doc in my C++ program. I know I can do this with C using libxml2 but I would like a C++ solution using libxml++ (which is built on top of libxml2).

Currently, I can parse from a file like so:

DomParser *parser = new DomParser;
parser->parse_file("sample.xml");
Community
  • 1
  • 1
Mike S
  • 11,329
  • 6
  • 41
  • 76
  • 1
    According to the docs there is a function to parse a string just like the function that parses a file: https://developer.gnome.org/libxml++/stable/classxmlpp_1_1DomParser.html#abe6b1966f057085047b7bc64ca3f064a – Galik Feb 16 '16 at 18:09
  • @Galik hmm not sure how I missed that... I'll give it a try. – Mike S Feb 16 '16 at 18:17

1 Answers1

0

The answer is simple (almost identical to parsing a file).

parser->parse_memory(xml_document);

In my case I needed the option to support both so this is the solution I came up with.

ifstream input_stream;
input_stream.open(xml.c_str(), ios::in); // attempt to open the file for reading 
if(input_stream.fail())
{
    parser->parse_memory(xml); // is not a file, parse as a string
} 
else
{
    parser->parse_file(xml); // is a file, parse as a file
}
Mike S
  • 11,329
  • 6
  • 41
  • 76