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.
 
int main()
{
using std::ofstream; //1
using std::ifstream;
using std::cout;
using std::endl;
 
book book1("To Love and Be Wise","Josephine Tey"); //2
book book2;
 
XMLPlatformUtils::Initialize(); //3
XalanTransformer::initialize(); //4
 
{
ofstream fout("book.xml"); //5
ifstream script("../XmlStreamOut.xsl"); //6
 
RWObjectOutputStream out = //7
RWXsltObjectOutputStreamImp::make(fout,script);
out << book1; //8
out.flush(); //9
}
//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.
NOTE: 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.
NOTE: Initialize the processor (in this example, Xalan) only once for a given process, no matter how many transformations are performed.