Comparison Example of Basic XML Streams with Enhanced XML Streams
Consider the following class.
class book {
RW_DECLARE_VIRTUAL_STREAM_FNS(book)
public:
book(const string& author, const string& title) : author_(author), title_(title) {}
string author_;
string title_;
};
// ...
RW_BEGIN_STREAM_CONTENTS(book)
RW_STREAM_ATTR_MEMBER(author,author_)
RW_STREAM_ATTR_MEMBER(title,title_)
RW_END_STREAM_CONTENTS
You can create an object of this class initialized as follows.
book b("Josephine Tey","To Love and Be Wise");
Then you can serialize the object with the original XML object streams as follows.
RWXmlObjectOutputStream out = RWXmlObjectOutputStreamImp::make(cout);
out << b;
This will produce the following XML document (namespace definitions have been omitted for brevity’s sake).
<rw:nested_object rw:class="book">
<rw:member rw:name="author" xsi:type="xsd:string">Josephine Tey</rw:member>
<rw:member rw:name="title" xsi:type="xsd:string">To Love and Be Wise</rw:member>
</rw:nested_object>
In order for the original XML input stream to extract such an object, the XML must appear exactly like the above code (allowing for the omitted namespace declarations). Any deviation, such as a missing attribute or an extra rw:member element, will cause the extraction to fail.
NOTE: The XML must be written in the exact form as is the above code block. If not, the input stream will be unable to perform the extraction.
However, with the enhanced XML stream, you use exactly the same pattern...
RWXmlObjectOutputStream out =
RWEnhancedXmlObjectOutputStreamImp::make(cout);
out << b;
...But you get a different result. The first of the following XML documents would be produced by the output stream, but any of them could be read successfully by the input stream (although the one with a missing element would produce an object with a different value, since the title_ member will be initialized to a default value).
<rw:nested_object rw:class="book" xsi:type="rw:nested_object">
<author xsi:type="xsd:string">Josephine Tey</author>
<title xsi:type="xsd:string">To Love and Be Wise</title>
</rw:nested_object>
<rw:nested_object rw:class="book" xsi:type="rw:nested_object">
<author xsi:type="xsd:string">Josephine Tey</author>
</rw:nested_object>
<rw:nested_object rw:class="book" xsi:type="rw:nested_object">
<author xsi:type="xsd:string">Josephine Tey</author>
<title xsi:type="xsd:string">To Love and Be Wise</title>
<isbn xsi:type="xsd:string">13413241324</isbn>
</rw:nested_object>
<rw:nested_object rw:class="book" xsi:type="rw:nested_object">
<title xsi:type="xsd:string">To Love and Be Wise</title>
<author xsi:type="xsd:string">Josephine Tey</author>
</rw:nested_object>
<rw:nested_object xsi:type="rw:nested_object" rw:class="book">
<author xsi:type="xsd:string">Josephine Tey</author>
<title xsi:type="xsd:string">To Love and Be Wise</title>
</rw:nested_object>
You can also use global function
RWInsertWithName() in
RWTInsertProxy (from the Advanced Tools Module) to give the book element an instance name. For example, you can rewrite the insertion as follows.
out << RWInsertWithName(b,"b");
Now you get the following document.
<b rw:class="book" xsi:type="rw:nested_object">
<author xsi:type="xsd:string">Josephine Tey</author>
<title xsi:type="xsd:string">To Love and Be Wise</title>
</b>
The following example shows how to use the enhanced XML object streams to serialize and deserialize a simple object.