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 [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]SetOutput(RBuf8&) method instead of the [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]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 [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]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.
XmlEnginePushL();
Create an instance of the serialiser by calling its [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]NewL() method.
The serialiser type is a member of the [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]TXmlEngSerializerType enumeration.CXmlEngSerializer* serializer = CXmlEngSerializer::NewL( ESerializerDefault );
Configure the serialiser by using the [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]SetOutputL() and [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]SetSerialisationOptions() methods.
You set the type of output (file, buffer, or stream) by choosing one of theSetOutput()
or SetOuputL()
methods.
_LIT( KOutputFile, "c:\\tutorial.xml" ); serializer->SetOutputL( KOutputFile );The
SetSerialisationOptions()
function sets other serialisation
options from the [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]TXmlEngSerializationOptions class.
TXmlEngSerializationOptions options( TXmlEngSerializationOptions::KOptionIndent ); serializer->SetSerializationOptions( options );
tutorial.xml
file.
TInt bytesWritten = serializer->SerializeL( myDoc );
The data in the myDoc
XML document
is stored in the tutorial.xml
file.