Symbian3/PDK/Source/GUID-AF937B6E-7BC0-5B7D-818E-93F7CAFD8808.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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
     1
<?xml version="1.0" encoding="utf-8"?>
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
     2
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
     3
<!-- This component and the accompanying materials are made available under the terms of the License 
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
     4
"Eclipse Public License v1.0" which accompanies this distribution, 
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
     5
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
     6
<!-- Initial Contributors:
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
     7
    Nokia Corporation - initial contribution.
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
     8
Contributors: 
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
     9
-->
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    10
<!DOCTYPE concept
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    11
  PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    12
<concept xml:lang="en" id="GUID-AF937B6E-7BC0-5B7D-818E-93F7CAFD8808"><title>Parsing an XML document using Content Handler</title><prolog><metadata><keywords/></metadata></prolog><conbody><p>This section explains how to parse an XML document by implementing the content handler functionality of the parser framework. </p> <p> <xref href="GUID-A6B8386B-29F6-3BEC-9D77-D8E0900DEAC2.dita"><apiname>MContentHandler</apiname></xref> class defines the interface required by a client of the XML framework. It allows a client to be placed in a chain with other clients such as a Parser or a Validator, and therefore allows the flow of information within chain. It provides callbacks similar to that of the SAX 2.0 interface. </p> <section><title>Introduction</title> <p>Consider a scenario when an application with numerous settings require the settings being saved, so that when the application is restarted after a shutdown, the settings are available. This can be achieved (while ensuring the cross-platform compatibility) by saving the setting in an XML file, which the application parses on start-up. The application responds to the XML statements by implementing the <xref href="GUID-A6B8386B-29F6-3BEC-9D77-D8E0900DEAC2.dita"><apiname>MContentHandler</apiname></xref> interface. Since the parser detects tags and their content, it calls the associated content handler functions to respond with the required behaviour. </p> <p>The callback functions which an implementation of <xref href="GUID-A6B8386B-29F6-3BEC-9D77-D8E0900DEAC2.dita"><apiname>MContentHandler</apiname></xref> must provide are depicted in the table. These correspond to functions defined in the <codeph>ContentHandler</codeph> interface of the <xref scope="external" href="http://www.saxproject.org">SAX specification</xref>. The last parameter of each function is an error code. If no error has taken place, then the <xref href="GUID-6CA4F1ED-7947-3087-B618-D35858FAA3BC.dita"><apiname>KErrNone</apiname></xref> null error code is returned by the framework. </p> <p> </p> <table id="GUID-BF14F3DB-7880-5299-874C-3B56554C8080"><tgroup cols="2"><colspec colname="col0"/><colspec colname="col1"/><thead><row><entry>MContentHandler callback</entry> <entry>SAX specification function</entry> </row> </thead> <tbody><row><entry><p> <xref href="GUID-6F240EEF-81FA-3A27-9593-A7ADC9118627.dita"><apiname>OnStartDocumentL()</apiname></xref>  </p> </entry> <entry><p> <codeph>startDocument() </codeph>  </p> </entry> </row> <row><entry><p> <xref href="GUID-81F0FD8C-825D-3283-B998-6B44CC3294FE.dita"><apiname>OnEndDocumentL()</apiname></xref>  </p> </entry> <entry><p> <codeph>endDocument() </codeph>  </p> </entry> </row> <row><entry><p> <xref href="GUID-5BFFB049-5C71-31FB-ADBE-89FF4296E125.dita"><apiname>OnStartElementL()</apiname></xref>  </p> </entry> <entry><p> <codeph>startElement()</codeph>  </p> </entry> </row> <row><entry><p> <xref href="GUID-6F1E258D-CE57-3C22-BBDE-AF5B6D6C90EB.dita"><apiname>OnEndElementL()</apiname></xref>  </p> </entry> <entry><p> <codeph>endElement() </codeph>  </p> </entry> </row> <row><entry><p> <xref href="GUID-37601F09-7C8A-3397-8750-92CE95C1B87B.dita"><apiname>OnContentL()</apiname></xref>  </p> </entry> <entry><p> <codeph>characters()</codeph>  </p> </entry> </row> <row><entry><p> <xref href="GUID-DA03C829-A9E3-3A8E-9367-208545FA0721.dita"><apiname>OnStartPrefixMappingL()</apiname></xref>  </p> </entry> <entry><p> <codeph>startPrefixMapping()</codeph>  </p> </entry> </row> <row><entry><p> <xref href="GUID-CC4C2F12-DC24-3F7F-BB5F-7D744F41E095.dita"><apiname>OnEndPrefixMappingL()</apiname></xref>  </p> </entry> <entry><p> <codeph> endPrefixMapping()</codeph>  </p> </entry> </row> <row><entry><p> <xref href="GUID-92826CB4-B56E-3E9F-BD4B-31ADE3974D91.dita"><apiname>OnIgnorableWhiteSpaceL()</apiname></xref>  </p> </entry> <entry><p> <codeph> ignorableWhitespace()</codeph>  </p> </entry> </row> <row><entry><p> <xref href="GUID-3D25881F-192F-3A21-B3DE-ECAB29C1D5CA.dita"><apiname>OnSkippedEntityL()</apiname></xref>  </p> </entry> <entry><p> <codeph>skippedEntity()</codeph>  </p> </entry> </row> <row><entry><p> <xref href="GUID-7DAF7316-BD31-3AD4-9854-F8CB6319D3D4.dita"><apiname>OnProcessingInstructionL()</apiname></xref>  </p> </entry> <entry><p> <codeph>processingInstruction()</codeph>  </p> </entry> </row> <row><entry><p> <xref href="GUID-BF4510DE-7C8B-3404-91C0-FAC737AA1A07.dita"><apiname>OnError()</apiname></xref>  </p> </entry> <entry><p> </p> </entry> </row> <row><entry><p> <xref href="GUID-A03D96B2-56B2-33A7-AC51-E1F5D1CA681C.dita"><apiname>GetExtendedInterface()</apiname></xref>  </p> </entry> <entry><p> </p> </entry> </row> </tbody> </tgroup> </table> <p>The following table lists the classes that are parameter of functions that must be implemented: </p> <table id="GUID-B34320E0-B8B1-5992-94AE-DA6D587D84F1"><tgroup cols="2"><colspec colname="col0"/><colspec colname="col1"/><thead><row><entry>Class</entry> <entry>Description</entry> </row> </thead> <tbody><row><entry><p> <xref href="GUID-D79A2F59-8DC1-3493-92F6-2E1337CD9405.dita"><apiname>RDocumentParameters</apiname></xref>  </p> </entry> <entry><p>Contains the character set which the document uses. </p> </entry> </row> <row><entry><p> <xref href="GUID-58047BD6-FECF-3BF0-869B-65F833D972A4.dita"><apiname>RAttributeArray</apiname></xref>  </p> </entry> <entry><p>Consists of an array of <xref href="GUID-BAD00F6C-6E5C-3331-BA58-85A186F933E8.dita"><apiname>RAttribute</apiname></xref> objects. The array holds the name (as an <xref href="GUID-6CEDFB6D-16B6-3860-922B-15A23C182DB2.dita"><apiname>RTagInfo</apiname></xref> object), value and type of each attribute of the element. </p> </entry> </row> <row><entry><p> <xref href="GUID-6CEDFB6D-16B6-3860-922B-15A23C182DB2.dita"><apiname>RTagInfo</apiname></xref>  </p> </entry> <entry><p>Contains information about an XML tag, its namespace URI and prefix, and its local name. </p> </entry> </row> </tbody> </tgroup> </table> </section> <section id="GUID-948C5BEE-0E05-525A-B601-909A1818FDA0"><title>Procedure</title> <p>Follow the steps given below to understand how to parse an XML document by implementing the content handler functionality: </p> <ol id="GUID-9404AB58-0C3C-5CE9-A4BE-D15861B145E8"><li id="GUID-D4F71FA7-3DC7-5CB7-89D6-459E4F35D43E"><p>Implement the <codeph>MContentHandler</codeph> interface. </p> <p>The following code snippet illustrates how to implement <codeph>MContentHandler</codeph>: </p> <codeblock id="GUID-DE2A07E6-00BD-577D-AF67-83B85CD59572" xml:space="preserve">class CMyContentHandler : public CBase, public MContentHandler
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    13
{
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    14
public:
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    15
    // A callback to indicate the start of the document. 
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    16
    void CXmlExample::OnStartDocumentL( const RDocumentParameters&amp;, TInt )
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    17
    {
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    18
    iConsole-&gt;Printf( KOnStartDoc );
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    19
    iConsole-&gt;Printf( KPressAKey );
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    20
    iConsole-&gt;Getch();
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    21
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    22
    iNumElements = 0;
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    23
    iNumSkippedEntities = 0;
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    24
    iNumPrefixMappings = 0;
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    25
    iNumPrefixUnmappings = 0;
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    26
    }
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    27
    
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    28
// A callback to indicate an element has been parsed.     
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    29
void CXmlExample::OnStartElementL( const RTagInfo&amp;, const RAttributeArray&amp;, TInt )
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    30
    {
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    31
    iConsole-&gt;Printf( KOnStartEle );
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    32
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    33
    if( iLeaveOnStartElement )
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    34
        {    
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    35
        if( iNumElements++ == 0 )
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    36
                  {
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    37
                  iConsole-&gt;Printf( KOnStartErr, KExpectedLeaveCode );
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    38
            User::After( 1 );
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    39
                  User::Leave( KExpectedLeaveCode );
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    40
                  }
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    41
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    42
           iNumElements++;
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    43
        }
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    44
    }
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    45
  
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    46
// implementations of the other callbacks
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    47
// ...
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    48
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    49
}</codeblock> </li> <li id="GUID-A6BB0985-37A3-526D-B678-266A55F57862"><p>Instantiate the <codeph>ContentHandler</codeph> object in the client application code and pass it to the constructor method of a parser object. </p> <codeblock id="GUID-14F1FCEB-7475-583B-867A-E7D19E8369B0" xml:space="preserve">...
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    50
CMyContentHandler* ch = CMyContentHandler::NewL();
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    51
CParser* parser = CParser::NewLC( KXmlMimeType,ch );
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    52
parser-&gt;ParseL( myXMLdata ); // this will result in callbacks to ch
25a17d01db0c Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608
Dominic Pinkman <Dominic.Pinkman@Nokia.com>
parents:
diff changeset
    53
...</codeblock> </li> </ol> </section> </conbody></concept>