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 [[[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.

Prerequisites

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.

Steps

  1. If you have not done it earlier, call the [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]XmlEnginePushL() method to open the XML library and put serialisation resources on the clean-up stack.
    XmlEnginePushL();
  2. 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 );
  3. 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 the SetOutput() 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 );
    
  4. Call the [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]SerializeL() method to save your document to the tutorial.xml file.
    TInt bytesWritten = serializer->SerializeL( myDoc );
  5. Free your resources as necessary.
    1. Close the XML library by calling the [[[ERROR: [NOKX000E] Unable to find definition for key reference 'XML']]]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.