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

6.3 Performing the Transformation

This transformation example uses a simple object named book. This object simply holds an author name and a book title. For the complete example, see buildspace\examples\xmlstreams\xsltTransform\xsltTransform.cpp.


This example is based on the examples used in Chapter 5.

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

6.3.1 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, you transform the output stream into 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 on.

//3

Initializes the Xerces XML parser, required by the Xalan transformation engine.


The use of other XSLT processors may require the initialization of a different parser.

//4

Initializes the Xalan transformation engine.

//5

Creates an ofstream for writing character data to a file.

//6

Creates an ifstream for reading in the output XSLT stylesheet.

//7

Sets up an XML output stream that includes support for a transformation by passing the ifstream to the output XSLT stylesheet.

//8

Serializes the book object to a file.

//9

Flushes the output buffer for the XML stream, transforming the XML in the process. You may perform this step explicitly, as it is performed by this line, or you may allow it to occur implicitly when the destructor calls flush(), and the output stream goes out of scope.


Initialize the processor (in this example, Xalan) only once for a given process, no matter how many transformations are performed.

6.3.2 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:

//1

Creates an ifstream for reading in the object data.

//2

Creates an ifstream for reading in the input XSLT stylesheet.

//3

Sets up an XML input stream that includes support for a transformation by passing the ifstream to the input XSLT stylesheet.

//4

Restores the book object as the book2 instance create earlier, transforming the XML back to the expected XML format in the process.

6.3.3 Examining the XML Output

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

//1

Creates an ifstream for reading in the output XSLT stylesheet.

//2

Sets up an XML output stream that includes support for a transformation by passing the ifstream to the output XSLT stylesheet.

//3

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

//4

Sets up an XML output stream with no transformation support.

//5

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.