2

I have seen tutorials all over the place for explaining how to get Code Synthesis xsd to work if you provide the xml in a file on your system, but I have not been able to find anything about providing the xml as a string.

I am receiving the xml from a TCP connection and I am attempting to parse it with Code Synthesis xsd, and it just seems like a useless additional step to create an xml file when I already have it in memory as a string.

And yes, this is in C++.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
PJB0515
  • 23
  • 5

1 Answers1

2

You can use std::istringstream to make a string appear as std::istream and then parse that:

#include <sstream>

std::string str = ... // Input XML in a string.
std::istringstream istr (str);

std::auto_ptr<root_type> r = root (istr);

Here root_type is the type and root is the name of the root element of your XML. The same approach works for serialization except you use std::ostringstream:

#include <sstream>

std::ostringstream ostr;

root (ostr, *r, ...);
std::string str = ostr.str () // Output XML in a string.
Boris Kolpackov
  • 674
  • 5
  • 6
  • 1
    A note: the parsing does validation by default, so the dreadded `instance document parsing failed` exception is thrown when the .xsd file can not be found in the executable path. To be on the safe side one might want to try instead: `root_ (istr, xml_schema::flags::dont_validate);`. – count0 Apr 16 '13 at 17:41
  • @count0: You can specify the XSD file with xml_schema::properties See for instance stackoverflow.com/a/11267720/757777 – Erik Sjölund Apr 24 '13 at 19:16