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

5.2 Transforming your XML Documents

5.2.1 Overview of the Transformation Process

The following sections illustrate how to perform both character-based and element-based transformations, using an object book.

The steps included in this section include:

5.2.2 Adding Serialization Support

The object book holds an author name and a book title.

First, you will instantiate book, and add serialization support. This process is identical for both transformations.

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

5.2.3 Serializing the book Object to a File

Next, you serialize your book object out to a file. Because the XML format generated by XML streams is difficult to read, transform the output stream into a more readable format.

Each of the following sections considers first a character-based C++ transformation, followed by an element-based C++ transformation.

5.2.3.1 For a Character-based C++ Transformation

//1Indicates that certain variables refer to their counterparts in the STD namespace.
//2Creates two book objects, one with data, one to hold the data when you restore the object later on.
//3Initializes the input stream transformation engine. (For the full code example, see MyInStreamTransform.h and MyInStreamTransform.cpp at buildspace\examples\xmlstreams\transform\.)
//4Initializes the output stream transformation engine (For the full code example, see MyOutStreamTransform.h and MyOutStreamTransform.cpp at buildspace\examples\xmlstreams\transform\.)
//5Creates an std::ofstream for writing character data to a file.
//6Sets up an XML output stream that includes support for a transformation.
//7Serializes the book object to a file.
//8Flushes the output buffer for the XML stream, transforming the XML in the process. You may perform this explicitly, as in this line, or you may let it happen implicitly when the destructor calls flush(), and the output stream goes out of scope.

5.2.3.2 For an Element-based C++ Transformation

//1Indicates that certain variables refer to their counterparts in the STD namespace.
//2Creates two book objects, one with data, one to hold the data when you restore the object later on.
//3Initializes the parsed transformation input stream. (For the full code example, see MyInParsedTransform.h and MyInParsedTransform.cpp at buildspace\examples\xmlstreams\transform\.)
//4Initializes the parsed transformation output stream. (For the full code example, see MyOutParsedTransform.h and MyOutParsedTransform.cpp at buildspace\examples\xmlstreams\transform\.)
//5Creates an std::ofstream for writing character data to a file.
//6Sets up an XML output stream that includes support for a transformation.
//7Serializes the book object to a file.
//8Flushes the output buffer for the XML stream, transforming the XML in the process. You may perform this explicitly, as in this line, or you may let it happen implicitly when the destructor calls flush(), and the output stream goes out of scope.

5.2.4 Restoring the book Object

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

5.2.4.1 For a Character-based C++ Transformation

//1Creates an std::ifstream for reading in the object data.
//2Sets up an XML input stream that includes support for a transformation.
//3Restores the book object as the book2 instance created earlier, transforming the XML back to the expected XML format in the process.

5.2.4.2 For an Element-based C++ Transformation

//1Creates an std::ifstream for reading in the object data.
//2Sets up an XML input stream that includes support for a transformation.
//3Restores the book object as the book2 instance create earlier, transforming the XML back to the expected XML format in the process.

5.2.5 Examining the XML Output

Finally, the example writes to standard out two forms of the serialized object:

5.2.5.1 For a Character-based C++ Transformation

//1Sets up an XML output stream that includes support for a transformation.
//2Streams the transformed version of the book2 object to standard out.
//3Sets up an XML output stream with no transformation support.
//4Streams the standard XML streams version of the book2 object to standard out.

Here is the resulting output:

With transformation:

Without transformation:

5.2.5.2 For an Element-based C++ Transformation

//1Sets up an XML output stream that includes support for a transformation.
//2Streams the transformed version of the book2 object to standard out.
//3Sets up an XML output stream with no transformation support.
//4Streams the standard XML streams version of the book2 object to standard out.

Here is the resulting output:

With transformation:

Without transformation:



Previous fileTop of DocumentContentsIndex pageNext file

© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.