Symbian3/PDK/Source/GUID-E20C2C98-A294-551B-99CF-BE1DAA6F597E.dita
author Dominic Pinkman <Dominic.Pinkman@Nokia.com>
Fri, 22 Jan 2010 18:26:19 +0000
changeset 1 25a17d01db0c
child 3 46218c8b8afa
permissions -rw-r--r--
Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License 
"Eclipse Public License v1.0" which accompanies this distribution, 
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
    Nokia Corporation - initial contribution.
Contributors: 
-->
<!DOCTYPE task
  PUBLIC "-//OASIS//DTD DITA Task//EN" "task.dtd">
<task id="GUID-E20C2C98-A294-551B-99CF-BE1DAA6F597E" xml:lang="en"><title>Serialising
XML DOM</title><shortdesc>This topic explains how to serialise a DOM document and save it
to a file. </shortdesc><prolog><metadata><keywords/></metadata></prolog><taskbody>
<prereq><p>Before you start, you must: </p> <ul>
<li id="GUID-128FA93B-B492-5F86-8E94-2DE1FDC3E29C"><p>understand the concepts
of <xref href="http://www.w3.org/DOM/" scope="external">Document Object Model
(DOM)</xref> and serialisation </p> </li>
<li id="GUID-B55CE275-DE00-5788-8D53-0F581078FFC9"><p>understand the structure
and classes of the <xref href="GUID-5ACFA8E4-3C4B-5851-A43E-9FA72D0B8403.dita">XML
DOM Engine </xref> component </p> </li>
<li id="GUID-75078E65-3FB2-5DC1-8D52-3D5B0E1A9674"><p>have created an <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>RXmlEngDocument</apiname></xref> (called <codeph>myDoc </codeph> in
this example), either by parsing an XML file (as indicated in the <xref href="GUID-C5C30218-311C-58D9-A990-3F95642F7517.dita">XML
DOM Parsing Tutorial</xref>) or by adding nodes to a new DOM tree. </p> </li>
</ul> </prereq>
<context> <p>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. </p> <p>This
tutorial explains how to save an existing XML document to a file. To store
the result in a buffer, use the <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>SetOutput(RBuf8&amp;)</apiname></xref> method
instead of the <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>SetOutputL(TDesC&amp;)</apiname></xref> method
used below. </p> </context>
<steps id="GUID-818D2E7F-4ECB-5687-AF68-2C02930519C7">
<step id="GUID-A241AD29-F46C-51DE-A30A-7F37AAACB5D4"><cmd/>
<info>If you have not done it earlier, call the <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>XmlEnginePushL()</apiname></xref> method
to open the XML library and put serialisation resources on the clean-up stack. </info>
<stepxmp><codeblock id="GUID-8CBCDA21-7DFB-5608-811F-4F9B126BD0A7" xml:space="preserve">XmlEnginePushL();</codeblock> </stepxmp>
</step>
<step id="GUID-3A35C368-F1CB-5183-8C4D-1E141F2B8ED4"><cmd>Create an instance
of the serialiser by calling its <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>NewL()</apiname></xref> method. </cmd>
<info>The serialiser type is a member of the <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>TXmlEngSerializerType</apiname></xref> enumeration. </info>
<stepxmp><codeblock id="GUID-587BB00E-579E-593F-B6CD-F58A548C2FBE" xml:space="preserve">CXmlEngSerializer* serializer = CXmlEngSerializer::NewL( ESerializerDefault );</codeblock> </stepxmp>
</step>
<step id="GUID-79270763-1081-57D8-9EB2-D21F7DF7035F"><cmd>Configure the serialiser
by using the <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>SetOutputL()</apiname></xref> and <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>SetSerialisationOptions()</apiname></xref> methods. </cmd>
<info>You set the type of output (file, buffer, or stream) by choosing one
of the <codeph>SetOutput()</codeph> or <codeph>SetOuputL()</codeph> methods. </info>
<stepxmp><codeblock id="GUID-5CCEBBCE-EE31-56B5-ADB7-06362097A90E" xml:space="preserve">_LIT( KOutputFile, "c:\\tutorial.xml" );
serializer-&gt;SetOutputL( KOutputFile );</codeblock> </stepxmp>
<info>The <codeph>SetSerialisationOptions()</codeph> function sets other serialisation
options from the <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>TXmlEngSerializationOptions</apiname></xref> class. </info>
<stepxmp><codeblock id="GUID-DF615E82-757C-518F-BDC7-55189153D4D7" xml:space="preserve">TXmlEngSerializationOptions options( TXmlEngSerializationOptions::KOptionIndent );
serializer-&gt;SetSerializationOptions( options );
</codeblock> </stepxmp>
</step>
<step id="GUID-77071B6A-3FAE-5BC6-BC91-FF998AC1E744"><cmd/>
<info>Call the <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>SerializeL()</apiname></xref> method to save
your document to the <filepath>tutorial.xml</filepath> file. </info>
<stepxmp><codeblock id="GUID-2358B434-3A2C-5EEE-A45D-1F0446CBC47F" xml:space="preserve">TInt bytesWritten = serializer-&gt;SerializeL( myDoc );</codeblock> </stepxmp>
</step>
<step id="GUID-EC0F4C51-CD58-5142-A17D-EB4FCAAA7099"><cmd/>
<info>Free your resources as necessary. </info>
<substeps id="GUID-17893DB6-AFBE-5E69-8FBD-F4010C7B0664">
<substep id="GUID-6475F251-42DE-51B2-A4DD-26195B95ACB2"><cmd/>
<info>Close the XML library by calling the <xref href="GUID-5748D958-9480-3358-A399-6B92293B86D9.dita"><apiname>XmlPopAndClose()</apiname></xref> method.
This method also removes the serialisation resources from the clean-up stack. </info>
</substep>
<substep id="GUID-3B286D3D-049D-52A2-893A-B5601A79C727"><cmd/>
<info>Perform any other clean-up operations. </info>
</substep>
</substeps>
</step>
</steps>
<result><p>The data in the <codeph>myDoc</codeph> XML document
is stored in the <filepath>tutorial.xml</filepath> file. </p> </result>
</taskbody></task>