Serialising XML DOM

This topic explains how to serialise a DOM document and save it to a file.

Context

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.

Prerequisites

Before you start, you must:

Steps

  1. If you have not done it earlier, call the XmlEnginePushL() method to open the XML library and put serialisation resources on the clean-up stack.

    Example:

    XmlEnginePushL();
  2. 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 );
  3. Configure the serialiser by using the SetOutputL() and SetSerialisationOptions() methods.

    You set the type of output (file, buffer, or stream) by choosing one of the SetOutput() or SetOuputL() methods.

    Example:

    _LIT( KOutputFile, "c:\\tutorial.xml" );
    serializer->SetOutputL( KOutputFile );
    The SetSerialisationOptions() function sets other serialisation options from the TXmlEngSerializationOptions class.

    Example:

    TXmlEngSerializationOptions options( TXmlEngSerializationOptions::KOptionIndent );
    serializer->SetSerializationOptions( options );
    
  4. Call the SerializeL() method to save your document to the tutorial.xml file.

    Example:

    TInt bytesWritten = serializer->SerializeL( myDoc );
  5. Free your resources as necessary.
    1. Close the XML library by calling the XmlPopAndClose() method. This method also removes the serialisation resources from the clean-up stack.
    2. Perform any other clean-up operations.

Results

The data in the myDoc XML document is stored in the tutorial.xml file.