Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
XML Streams Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

4.3 Example

This example uses a simple object named book that holds an author name and a book title:

As described in the example in Chapter 3, you must add macros to the header and implementation files to prepare the object for serialization.

Now you are ready to look at the example itself.

4.3.1 Example Discussion

This section explains the main() function for the example. The explanation is broken into three parts:

For the complete example, see:

buildspace\examples\xmlstreams\xsltTransform\enhanced.cpp

4.3.2 Serializing the book Object to a File

First create a book object and serialize it out to a file. Because the XML format generated by XML streams is difficult to read, transform the output stream to a more easily-readable format.

//1

Indicates that certain variables refer to their counterparts in the STD namespace.

//2

Creates two book objects, one with data, one to hold the data when you restore the object later.

//3

Creates an ofstream for writing character data to a file.

//4

Sets up an enhanced XML output stream that includes support for a transformation.

//5

Serializes the book object to a file.

4.3.3 Restoring the book Object

At this point you have written the book object XML data to the file book.xml. Next restore the object, which involves:

//1

Creates an ifstream for reading in the object data.

//2

Sets up an enhanced XML input stream.

//3

Restores the book object as the book2 instance create earlier.

4.3.4 Examining the XML Output

Finally, the example writes to standard out two forms of the serialized object. You'll also use the rwInsertWithName() global method in order to provide an instance name for the book object.

//1

Sets up an enhanced XML output stream.

//2

Streams the enhanced version of the book2 object to standard out.

//3

Sets up an XML output stream with no transformation support.

//4

Streams the standard XML streams version of the book2 object to standard out.

Here is the resulting output:

In enhanced XML stream format:

In ordinary XML stream format:

If you need to control the format of the data within the XML stream, the streams classes have a make() function that takes a pointer reference to an std::ios object. The constructor for the XML stream fills this pointer with the address of a formatting object for the underlying character stream. Here is an example for the class RWEnhancedXmlObjectOutputStreamImp.

The following classes contain a make() function that supports a formatting object:



Previous fileTop of DocumentContentsIndex pageNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.