Code Synthesis XSD parsing/data binding xml string instead of xml file

Go To StackoverFlow.com

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++.

2012-04-05 01:57
by PJB0515


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.
2012-04-06 11:24
by Boris Kolpackov
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 2013-04-16 17:41
@count0: You can specify the XSD file with xml_schema::properties See for instance stackoverflow.com/a/11267720/75777 - Erik Sjölund 2013-04-24 19:16
Ads