This topic explains how to serialise a DOM document and save it to a file.
The XML Engine is based on the libxml2 library, which supports the serialisation of XML documents. This process converts a DOM tree into a format that can be saved to a file or transferred through a network connection.
This tutorial explains how to save an existing XML document to a file. To store the result in a buffer, use the SetOutput(RBuf8&) method instead of the SetOutputL(TDesC&) method used below.
Before you start, you must:
understand the concepts of Document Object Model (DOM) and serialisation
understand the structure and classes of the XML DOM Engine component
have created an RXmlEngDocument (called myDoc
in
this example), either by parsing an XML file (as indicated in the XML
DOM Parsing Tutorial) or by adding nodes to a new DOM tree.
Example:
XmlEnginePushL();
Create an instance of the serialiser by calling its NewL() method.
The serialiser type is a member of the TXmlEngSerializerType enumeration.Example:
CXmlEngSerializer* serializer = CXmlEngSerializer::NewL( ESerializerDefault );
Configure the serialiser by using the SetOutputL() and SetSerialisationOptions() methods.
You set the type of output (file, buffer, or stream) by choosing one of theSetOutput()
or SetOuputL()
methods.
Example:
_LIT( KOutputFile, "c:\\tutorial.xml" ); serializer->SetOutputL( KOutputFile );
SetSerialisationOptions()
function sets other serialisation
options from the TXmlEngSerializationOptions class.
Example:
TXmlEngSerializationOptions options( TXmlEngSerializationOptions::KOptionIndent ); serializer->SetSerializationOptions( options );
tutorial.xml
file.
Example:
TInt bytesWritten = serializer->SerializeL( myDoc );
The data in the myDoc
XML document
is stored in the tutorial.xml
file.