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.
NOTE >> This example is based on the examples used in
Chapter 5.
class book
{
public:
book ();
book (const RWCString& title, const RWCString& author);
private:
RWCString title_;
RWCString author_;
};
This section discusses the main() function for the example. The explanation is broken into three parts:
• creating a book object and serializing it to a file using an XML stream that applies a transformation to make the resulting XML more readable
• reading the file back in and restoring the book object using an XML stream that applies a transformation to restore the original XML format
• printing out both the original XML streams data for the book object and its more readable, transformed version
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
}
NOTE >> The use of other XSLT processors may require the initialization of a different parser.
NOTE >> Initialize the processor (in this example, Xalan) only once for a given process, no matter how many transformations are performed.
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:
• reading in the file book.xml
• transforming the XML back to the form required by XML streams
• restoring the object as book2
{
ifstream fin("book.xml"); //1
ifstream script("../XmlStreamIn.xsl"); //2
RWObjectInputStream in = //3
RWXsltObjectInputStreamImp::make(fin,script);
in >> book2; //4
}
Examining the XML Output
Finally, the example writes to standard out two forms of the serialized object:
• the transformed, more readable version
• the form created by the XML serialization and expected by XML streams when the object is restored
{
ifstream script("../XmlStreamOut.xsl"); //1
RWObjectOutputStream out = //2
RWXsltObjectOutputStreamImp::make(cout,script);
out << book2; //3
}
cout << endl;
{
RWObjectOutputStream out = //4
RWXmlObjectOutputStreamImp::make(cout);
out << book2; //5
}
return 0;
}
Here is the resulting output:
With transformation:
<?xml version="1.0" encoding="UTF-8"?>
<book xmlns:rw="http://www.roguewave.com/xmlstream"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xsi:type="book">
<title xsi:type="xsd:string">To Love and Be Wise</title>
<author xsi:type="xsd:string">Josephine Tey</author>
</book>
Without transformation:
<?xml version="1.0" standalone="yes"?>
<rw:nested_object rw:class="book"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:rw="http://www.roguewave.com/xmlstream">
<rw:member rw:name="title" xsi:type="xsd:string">To Love and Be
Wise</rw:member>
<rw:member rw:name="author" xsi:type="xsd:string">Josephine
Tey</rw:member>
</rw:nested_object>