[xsd-users] XML Object to String/Text
Paddy Vishnubhatt
Paddy.Vishnubhatt at 3leafnetworks.com
Wed Feb 21 10:47:29 EST 2007
Thx Boris for the prompt response.
________________________________
From: Boris Kolpackov [mailto:boris at codesynthesis.com]
Sent: Wed 2/21/2007 7:10 AM
To: Paddy Vishnubhatt
Cc: xsd-users at codesynthesis.com
Subject: Re: [xsd-users] XML Object to String/Text
Hi Paddy,
Paddy Vishnubhatt <Paddy.Vishnubhatt at 3leafnetworks.com> writes:
> 1. is there a simple way to get a stringified representation of the
> object? Like in the Java API (xmlbeans), xmlText() or even toString()
> which outputs a Sring representation of the given Xml Object?
You can use one of the serialization functions together with
std::ostringstream. For instance, in the library example there is
the this line:
// Write it out.
//
catalog_ (std::cout, *c, map);
You can replace it with something like this (also add #include <sstream>):
// Write it to string.
//
std::ostringstream ostr;
catalog_ (ostr, *c, map);
std::string str = ostr.str (); // XML is in str.
>>
In my xsd definition and compliation (i did use the --generate-serialization per yr note in the example), I don't find the catalog_ definitions/declarations. (I get an catalog_ undeclared compile error). In order to use route 1, is it a must to create a document object in the XSD definition like how you did it in library example?
(xsd:element name="catalog" type="lib:catalog"/>)
If so, What if you don't have a documentation declaration in xsd (I have many xsd definitions and would like to avoid creating document objects for each one of them).
> 2. I see that there are documented ways to create DOMDocument from which
> one can use DOMWriter to use writeString to get to ultimately get to
> the string - is there an example/sample code that I can use for/as
> reference.
There is some sample DOM manipulation code on the Wiki, see for example
3.1 and 3.2:
http://wiki.codesynthesis.com/Tree/FAQ
The ostringstream way is much easier though.
>> True, ostringstream is definitely the right approach but am wondering if I need to do something different w/ my xsd definitions...
More information about the xsd-users
mailing list