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

This section describes how to transform 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 5, 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

//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 on.

//3

Initializes the input stream transformation engine. (For the full code example, see MyInStreamTransform.h and MyInStreamTransform.cpp at buildspace\examples\xmlstreams\transform\.)

//4

Initializes the output stream transformation engine (For the full code example, see MyOutStreamTransform.h and MyOutStreamTransform.cpp at buildspace\examples\xmlstreams\transform\.)

//5

Creates an ofstream for writing character data to a file.

//6

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

//7

Serializes the book object to a file.

//8

Flushes 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

//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 on.

//3

Initializes the parsed transformation input stream. (For the full code example, see MyInParsedTransform.h and MyInParsedTransform.cpp at buildspace\examples\xmlstreams\transform\.)

//4

Initializes the parsed transformation output stream. (For the full code example, see MyOutParsedTransform.h and MyOutParsedTransform.cpp at buildspace\examples\xmlstreams\transform\.)

//5

Creates an ofstream for writing character data to a file.

//6

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

//7

Serializes the book object to a file.

//8

Flushes 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

//1

Creates an ifstream for reading in the object data.

//2

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

//3

Restores 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

//1

Creates an ifstream for reading in the object data.

//2

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

//3

Restores 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

//1

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

//2

Streams the transformed 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:

With transformation:

Without transformation:

5.2.5.2 For an Element-based C++ Transformation

//1

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

//2

Streams the transformed 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:

With transformation:

Without transformation:



Previous fileTop of DocumentContentsNo linkNext 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.
Provide feedback to Rogue Wave about its documentation.