vpnengine/utlxml/src/XppTest.cpp
changeset 0 33413c0669b9
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     1 /*
       
     2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: XML pull parser tester.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <e32cons.h>
       
    21 #include <badesca.h>
       
    22 #include "XppImpl.h"
       
    23 #include "XwImpl.h"
       
    24 
       
    25 _LIT(KTxtXppTest, "XppTest");
       
    26 _LIT(KTxtTitle, "XML pull parser tester");
       
    27 _LIT(KTxtFailed, "Failed: leave code = %d");
       
    28 _LIT(KTxtOK, "OK");
       
    29 _LIT(KTxtPressAnyKey, " [press any key]");
       
    30 
       
    31 LOCAL_D CConsoleBase* console;
       
    32 
       
    33 LOCAL_C void callExampleL();
       
    34 LOCAL_C void doExampleL();
       
    35 LOCAL_C void XmlParsingExampleL();
       
    36 LOCAL_C void XmlWritingExampleL();
       
    37 
       
    38 // The starting point
       
    39 GLDEF_C TInt E32Main()
       
    40     {
       
    41     __UHEAP_MARK;
       
    42     
       
    43     CTrapCleanup* cleanup = CTrapCleanup::New();
       
    44     TRAPD(error, callExampleL());
       
    45     __ASSERT_ALWAYS(!error, User::Panic(KTxtXppTest, error));
       
    46     delete cleanup;
       
    47     
       
    48     __UHEAP_MARKEND;
       
    49     
       
    50     return 0;
       
    51     }
       
    52 
       
    53 // Console initialization and the doExample call  
       
    54 LOCAL_C void callExampleL()
       
    55     {
       
    56     console = Console::NewL(KTxtTitle, TSize(KConsFullScreen, KConsFullScreen));
       
    57     CleanupStack::PushL(console);
       
    58     
       
    59     TRAPD(error, doExampleL());
       
    60     
       
    61     if (error)
       
    62         console->Printf(KTxtFailed, error);
       
    63     else
       
    64         console->Printf(KTxtOK);
       
    65     
       
    66     console->Printf(KTxtPressAnyKey);
       
    67     console->Getch();
       
    68     
       
    69     CleanupStack::PopAndDestroy(); // console
       
    70     }
       
    71 
       
    72 //----------------------------------------------------------------------------- 
       
    73 // XML parsing example
       
    74 //----------------------------------------------------------------------------- 
       
    75     
       
    76 // The test XML document
       
    77 
       
    78 _LIT8(KTestXML,
       
    79      "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?> \
       
    80       <!--  This is a comment that should be skipped when parsing --> \
       
    81       <!--  This is the 2nd comment that should be skipped when parsing --> \
       
    82       <!DOCTYPE note [ \
       
    83           <!ELEMENT note (to, from, heading, body)> \
       
    84           <!ELEMENT to (#PCDATA)> \
       
    85           <!ELEMENT from (#PCDATA)> \
       
    86           <!ELEMENT heading (#PCDATA)> \
       
    87           <!ELEMENT body (#PCDATA)> \
       
    88       ]> \
       
    89       <Metadata xmlns='&#ab;attr1&#x5d; acu' attr2=\"second&#xerror;attribute\" > \
       
    90           <Name  ><![CDATA[<1st cdata section>]]>name &#x3f; 1</Name > \
       
    91           <Name>&#110;&#97;&#x6D;&#x65;&#x20;&#x32;<![CDATA[</2dn cdata section>&lt;&#x3e;]]></Name> \
       
    92           <Name>name 3 start<![CDATA[]</3rd cdata section>]]>name 3 end</Name > \
       
    93           <Type>a&lt;b&#x3C; c &gt;d&#x7b;e&amp;f&#35;g&apos;h&#x2F;i&quot;j&#x7f;</Type> \
       
    94           <Id>12345</Id> \
       
    95       </Metadata> \
       
    96       <!--  This is the 3rd comment -->");
       
    97 
       
    98 // The second test XML document
       
    99 _LIT8(KTestXML2,
       
   100       "<VPNPolicyPkg><File name='nokia_nic_vpn-sales-1_0.pin'>data1</File><File name='nokia_nic_vpn-sales-1_0.pol'>data2</File></VPNPolicyPkg>");
       
   101 
       
   102 // 2nd Tag/attribute names
       
   103 _LIT8(KTagVPNPolicyPkg, "/VPNPolicyPkg");
       
   104 _LIT8(KTagFile, "/VPNPolicyPkg/File");
       
   105 _LIT8(KAttrName, "name");
       
   106 
       
   107 // Tag/attribute names
       
   108 _LIT8(KTagMetadata, "/Metadata");
       
   109 _LIT8(KTagName, "/Metadata/Name");
       
   110 _LIT8(KTagType, "/Metadata/Type");
       
   111 _LIT8(KTagId, "/Metadata/Id");
       
   112 _LIT8(KAttrXmlns, "xmlns");
       
   113 _LIT8(KAttrAttr2, "attr2");
       
   114 
       
   115 
       
   116 _LIT8(KTestXML3, 
       
   117       "<sessions esgId=\"44\" lang=\"FI\" modified=\"3261983767\" expiryTime=\"3262005367\"/>");
       
   118 
       
   119 // 3rd Tag/attribute names
       
   120 _LIT8(KTagSessions, "/sessions");
       
   121 _LIT8(KAttrEsgId, "esgId");
       
   122 _LIT8(KAttrLang, "lang");
       
   123 _LIT8(KAttrModified, "modified");
       
   124 _LIT8(KAttrExpiryTime, "expiryTime");
       
   125 
       
   126 
       
   127 // Outputting formatting
       
   128 _LIT(KFormatName, "Name: %S\n");
       
   129 _LIT(KPosition, "Position: %u\n");
       
   130 _LIT(KStartPosition, "Element Start Position: %u\n");
       
   131 _LIT(KLength, "Total length: %u\n");
       
   132 _LIT(KFormatType, "Type: %S\n");
       
   133 _LIT(KFormatId, "Id: %S\n");
       
   134 _LIT(KFormatXmlns, "Id: %S\n");
       
   135 _LIT(KFormatAttr2, "attr2: %S\n");
       
   136 _LIT(KFormatAttrName, "Attr Name: %S\n");
       
   137 _LIT(KFormatEndTag, "End tag: %S\n");
       
   138 _LIT(KFormatEndDocumentEndTag, "End doc end tag: %S\n");
       
   139 _LIT(KFormatPart, "Document part: %S\n");
       
   140 _LIT(KFormatError, "Error\n");
       
   141 
       
   142 // Output format for 3rd test
       
   143 _LIT(KFormatAttrEsgId, "esgId: %S\n");
       
   144 _LIT(KFormatAttrLang, "lang: %S\n");
       
   145 _LIT(KFormatAttrModified, "modified: %S\n");
       
   146 _LIT(KFormatAttrExpiryTime, "expiryTime: %S\n");
       
   147 
       
   148 
       
   149 // The actual example
       
   150 LOCAL_C void doExampleL()
       
   151     {
       
   152     // To make code scanner silent
       
   153     TRAP_IGNORE(XmlWritingExampleL());
       
   154     }
       
   155 
       
   156 LOCAL_C void XmlParsingExampleL()
       
   157     {
       
   158     CDesC8ArrayFlat* stringValueArray = new (ELeave) CDesC8ArrayFlat(1);  // jakovist test
       
   159     // Create the parser
       
   160     CXmlPullParser* parser = CXmlPullParser::NewLC(*stringValueArray);
       
   161     //CXmlPullParser* parser = CXmlPullParser::NewLC();
       
   162 
       
   163     // Specify the XML document to parse
       
   164     parser->SetInput(KTestXML);
       
   165     //parser->SetInput(KTestXML2);
       
   166 
       
   167     // Pull all interesting data from the document...
       
   168     CXmlPullParser::TParserState state;
       
   169 
       
   170     do
       
   171         {
       
   172         User::LeaveIfError(parser->NextL());
       
   173 
       
   174         state = parser->State();
       
   175 
       
   176         TPtrC8 path;
       
   177         parser->PathL(path);
       
   178 
       
   179         TPtrC8 value;
       
   180 
       
   181         // needed in console print
       
   182         //TBuf<1024> copyBuf;
       
   183         TBuf<512> copyBuf;
       
   184 
       
   185 //#if 0 //test
       
   186         // Get and show element texts
       
   187         if (state == CXmlPullParser::EStateText)
       
   188             {
       
   189             if (path.Compare(KTagName) == 0)
       
   190                 {
       
   191                 TInt retVal = parser->TextL(value);
       
   192                 if (retVal == KErrNone)
       
   193                     {
       
   194                     copyBuf.Copy(value);
       
   195                     console->Printf(KFormatName, &copyBuf);
       
   196                     //console->Printf(KStartPosition, parser->CurrentElementPos());
       
   197                     //console->Printf(KPosition, parser->Pos());
       
   198                     }
       
   199                 }
       
   200             if (path.Compare(KTagType) == 0)
       
   201                 {
       
   202                 TInt retVal = parser->TextL(value);
       
   203                 if (retVal == KErrNone)
       
   204                     {
       
   205                     copyBuf.Copy(value);
       
   206                     console->Printf(KFormatType, &copyBuf);
       
   207                     //console->Printf(KStartPosition, parser->CurrentElementPos());
       
   208                     //console->Printf(KPosition, parser->Pos());
       
   209                     }
       
   210                 retVal = parser->TextPredefEntitiesNotEscaped(value);
       
   211                 if (retVal == KErrNone)
       
   212                     {
       
   213                     copyBuf.Copy(value);
       
   214                     console->Printf(KFormatType, &copyBuf);
       
   215                     }
       
   216                 }
       
   217             if (path.Compare(KTagId) == 0)
       
   218                 {
       
   219                 parser->TextL(value);
       
   220                 copyBuf.Copy(value);
       
   221                 console->Printf(KFormatId, &copyBuf);
       
   222                 //console->Printf(KStartPosition, parser->CurrentElementPos());
       
   223                 //console->Printf(KPosition, parser->Pos());
       
   224                 }
       
   225             }
       
   226 //#endif  // if 0 
       
   227 
       
   228         // Get and shown an attribute value
       
   229         if (state == CXmlPullParser::EStateStartTag)
       
   230             {
       
   231             if (path.Compare(KTagMetadata) == 0)
       
   232                 {
       
   233                 TInt retVal = parser->AttributeValueL(KAttrXmlns, value);
       
   234                 if (retVal == KErrNone)
       
   235                     {
       
   236                     copyBuf.Copy(value);
       
   237                     console->Printf(KFormatXmlns, &copyBuf);
       
   238                     //console->Printf(KStartPosition, parser->CurrentElementPos());
       
   239                     //console->Printf(KPosition, parser->Pos());
       
   240                     }
       
   241 
       
   242                 retVal = parser->AttributeValuePredefEntitiesNotEscaped(KAttrXmlns, value);
       
   243                 if (retVal == KErrNone)
       
   244                     {
       
   245                     copyBuf.Copy(value);
       
   246                     console->Printf(KFormatXmlns, &copyBuf);
       
   247                     }
       
   248 
       
   249                 
       
   250                 retVal = parser->AttributeValueL(KAttrAttr2, value);
       
   251                 if (retVal == KErrNone)
       
   252                     {
       
   253                     copyBuf.Copy(value);
       
   254                     console->Printf(KFormatAttr2, &copyBuf);
       
   255                     }
       
   256 
       
   257                 TInt attrCount;
       
   258                 parser->AttributeCount(attrCount);
       
   259                 retVal = parser->AttributeValueL((attrCount - 1), value);
       
   260                 if (retVal == KErrNone)
       
   261                     {
       
   262                     copyBuf.Copy(value);
       
   263                     console->Printf(KFormatAttr2, &copyBuf);
       
   264                     }
       
   265 
       
   266 /*
       
   267                 // Unsuccessfull attribute name getting tests
       
   268                 retVal = parser->AttributeName(-1, value); // negative index value
       
   269                 if (retVal == KErrNone)
       
   270                     {
       
   271                     copyBuf.Copy(value);
       
   272                     console->Printf(KFormatAttrName, &copyBuf);
       
   273                     }
       
   274                 else
       
   275                     {
       
   276                     console->Printf(KFormatError);
       
   277                     }
       
   278 
       
   279                 retVal = parser->AttributeName(2, value); // index value too big
       
   280                 if (retVal == KErrNone)
       
   281                     {
       
   282                     copyBuf.Copy(value);
       
   283                     console->Printf(KFormatAttrName, &copyBuf);
       
   284                     }
       
   285                 else
       
   286                     {
       
   287                     console->Printf(KFormatError);
       
   288                     }
       
   289 
       
   290                 // Unsuccessfull attribute getting tests
       
   291                 retVal = parser->AttributeValueL(-1, value); // negative index value
       
   292                 if (retVal == KErrNone)
       
   293                     {
       
   294                     copyBuf.Copy(value);
       
   295                     console->Printf(KFormatXmlns, &copyBuf);
       
   296                     }
       
   297                 else
       
   298                     {
       
   299                     console->Printf(KFormatError);
       
   300                     }
       
   301 
       
   302                 retVal = parser->AttributeValueL(2, value); // index value too big
       
   303                 if (retVal == KErrNone)
       
   304                     {
       
   305                     copyBuf.Copy(value);
       
   306                     console->Printf(KFormatXmlns, &copyBuf);
       
   307                     }
       
   308                 else
       
   309                     {
       
   310                     console->Printf(KFormatError);
       
   311                     }
       
   312 */
       
   313                 }
       
   314 
       
   315             //Second test
       
   316             if (path.Compare(KTagFile) == 0)
       
   317                 {
       
   318                 TInt retVal = parser->AttributeValueL(KAttrName, value);
       
   319                 if (retVal == KErrNone)
       
   320                     {
       
   321                     copyBuf.Copy(value);
       
   322                     console->Printf(KFormatXmlns, &copyBuf);
       
   323                     //console->Printf(KStartPosition, parser->CurrentElementPos());
       
   324                     //console->Printf(KPosition, parser->Pos());
       
   325                     }
       
   326                 }
       
   327             }
       
   328 
       
   329         // Get and show a part of document being parsed
       
   330 
       
   331         if (state == CXmlPullParser::EStateEndDocument)
       
   332             {
       
   333             TPtrC8 docPart = parser->DocPart(30, 31);  // aStartPos and aEndPos just an example
       
   334             copyBuf.Copy(docPart);
       
   335             console->Printf(KFormatPart, &copyBuf);
       
   336             }
       
   337 
       
   338         // Get and show end tag value
       
   339 /*        if (state == CXmlPullParser::EStateEndTag)
       
   340             {
       
   341             if (path.Compare(KTagMetadata) == 0)
       
   342                 {
       
   343                 TInt retVal = parser->Name(value);
       
   344                 if (retVal == KErrNone)
       
   345                     {
       
   346                     copyBuf.Copy(value);
       
   347                     console->Printf(KFormatEndTag, &copyBuf);
       
   348                     console->Printf(KPosition, parser->Pos());
       
   349                     }
       
   350                 }
       
   351             }
       
   352 */
       
   353 
       
   354         // Get and show end tag value
       
   355 /*
       
   356         if (state == CXmlPullParser::EStateEndDocument)
       
   357             {
       
   358             TInt retVal = parser->Name(value);
       
   359             if (retVal == KErrNone)
       
   360                 {
       
   361                 copyBuf.Copy(value);
       
   362                 console->Printf(KFormatEndDocumentEndTag, &copyBuf);
       
   363                 console->Printf(KLength, parser->Length());
       
   364                 }
       
   365             }
       
   366 */
       
   367         
       
   368         }while (state != CXmlPullParser::EStateEndDocument);
       
   369 
       
   370 #if 0
       
   371     // this is for testing new feature: same parser instance can 
       
   372     // be used
       
   373     parser->SetInput(KTestXML2);
       
   374 
       
   375     // Pull all interesting data from the document...
       
   376     //CXmlPullParser::TParserState state;
       
   377 
       
   378     do
       
   379         {
       
   380         User::LeaveIfError(parser->NextL());
       
   381 
       
   382         state = parser->State();
       
   383 
       
   384         TPtrC8 path;
       
   385         parser->PathL(path);
       
   386 
       
   387         TPtrC8 value;
       
   388 
       
   389         // needed in console print
       
   390         TBuf<512> copyBuf;
       
   391 
       
   392         // Get and show element texts
       
   393         if (state == CXmlPullParser::EStateText)
       
   394             {
       
   395             if (path.Compare(KTagVPNPolicyPkg) == 0)
       
   396                 {
       
   397                 parser->TextL(value);
       
   398                 copyBuf.Copy(value);
       
   399                 console->Printf(KFormatName, &copyBuf);
       
   400                 }
       
   401             if (path.Compare(KTagFile) == 0)
       
   402                 {
       
   403                 parser->TextL(value);
       
   404                 copyBuf.Copy(value);
       
   405                 console->Printf(KFormatType, &copyBuf);
       
   406                 }
       
   407             }
       
   408 
       
   409         // Get and shown an attribute value
       
   410         if (state == CXmlPullParser::EStateStartTag)
       
   411             {
       
   412             if (path.Compare(KTagFile) == 0)
       
   413                 {
       
   414                 TInt retVal = parser->AttributeValueL(KAttrName, value);
       
   415                 if (retVal == KErrNone)
       
   416                     {
       
   417                     copyBuf.Copy(value);
       
   418                     console->Printf(KFormatAttrName, &copyBuf);
       
   419                     }
       
   420                 }
       
   421             }
       
   422 
       
   423         // Get and show a part of document being parsed
       
   424         if (state == CXmlPullParser::EStateEndDocument)
       
   425             {
       
   426             TPtrC8 docPart = parser->DocPart(0, 10);  // aStartPos and aEndPos just an example
       
   427             copyBuf.Copy(docPart);
       
   428             console->Printf(KFormatPart, &copyBuf);
       
   429             }
       
   430         } while (state != CXmlPullParser::EStateEndDocument);
       
   431 
       
   432 
       
   433     // Test number 3:
       
   434 
       
   435     parser->SetInput(KTestXML3);
       
   436     // Pull all interesting data from the document...
       
   437     //CXmlPullParser::TParserState state;
       
   438 
       
   439     do
       
   440         {
       
   441         User::LeaveIfError(parser->NextL());
       
   442 
       
   443         state = parser->State();
       
   444 
       
   445         TPtrC8 path;
       
   446         parser->PathL(path);
       
   447 
       
   448         TPtrC8 value;
       
   449 
       
   450         // needed in console print
       
   451         TBuf<512> copyBuf;
       
   452 
       
   453         // Get and shown an attribute value
       
   454         if (state == CXmlPullParser::EStateStartTag)
       
   455             {
       
   456             if (path.Compare(KTagSessions) == 0)
       
   457                 {
       
   458                 TInt retVal = parser->AttributeValueL(KAttrEsgId, value);
       
   459                 if (retVal == KErrNone)
       
   460                     {
       
   461                     copyBuf.Copy(value);
       
   462                     console->Printf(KFormatAttrEsgId, &copyBuf);
       
   463                     }
       
   464                 retVal = parser->AttributeValueL(KAttrLang, value);
       
   465                 if (retVal == KErrNone)
       
   466                     {
       
   467                     copyBuf.Copy(value);
       
   468                     console->Printf(KFormatAttrLang, &copyBuf);
       
   469                     }
       
   470                 retVal = parser->AttributeValueL(KAttrModified, value);
       
   471                 if (retVal == KErrNone)
       
   472                     {
       
   473                     copyBuf.Copy(value);
       
   474                     console->Printf(KFormatAttrModified, &copyBuf);
       
   475                     }
       
   476                 retVal = parser->AttributeValueL(KAttrExpiryTime, value);
       
   477                 if (retVal == KErrNone)
       
   478                     {
       
   479                     copyBuf.Copy(value);
       
   480                     console->Printf(KFormatAttrExpiryTime, &copyBuf);
       
   481                     }
       
   482                 }
       
   483             }
       
   484 
       
   485         // Get and show a part of document being parsed
       
   486         if (state == CXmlPullParser::EStateEndDocument)
       
   487             {
       
   488             TPtrC8 docPart = parser->DocPart(0, 10);  // aStartPos and aEndPos just an example
       
   489             copyBuf.Copy(docPart);
       
   490             console->Printf(KFormatPart, &copyBuf);
       
   491             }
       
   492         } while (state != CXmlPullParser::EStateEndDocument);
       
   493 #endif  // if 0
       
   494 
       
   495     delete stringValueArray;
       
   496     CleanupStack::PopAndDestroy(); // parser
       
   497     }
       
   498 
       
   499 //----------------------------------------------------------------------------- 
       
   500 // XML writing example
       
   501 //----------------------------------------------------------------------------- 
       
   502 
       
   503 // Tag/attribute names and values
       
   504 _LIT8(KVerNum, "1.0");
       
   505 _LIT8(KTagRoot, "root");
       
   506 _LIT8(KTagElement1, "element1");
       
   507 _LIT8(KTagElement2, "element2");
       
   508 _LIT8(KTagElement3, "element3");
       
   509 _LIT8(KAttr1, "attr1");
       
   510 _LIT8(KAttr2, "attr2");
       
   511 _LIT8(KVal1, "val1<<a>b& \'1\' \"2\"");
       
   512 _LIT8(KVal2, "val2");
       
   513 _LIT8(KText, "text a<b x&z t>n \'g\' \"99\"&&><");
       
   514 _LIT8(KGreeting, "<greeting>");
       
   515     
       
   516 LOCAL_C void XmlWritingExampleL()
       
   517     {
       
   518     /*
       
   519      * Create a document like this (newlines
       
   520      * and indentation will no appear in the
       
   521      * actual output):
       
   522      *
       
   523      * <?xml version="1.0" encoding="ISO-8859-1"?>
       
   524      * <root>
       
   525      *     <element1>
       
   526      *         text a<b x&z t>n \'g\' \"99\"&&><
       
   527      *     </element1>
       
   528      *     <element2 attr1='val1&lt;&lt;b&amp; &apos;1&apos; &quot;2&quot;' attr2='val2'/>
       
   529      *     <element3 attr1='val1&lt;&lt;b&amp; &apos;1&apos; &quot;2&quot;'>
       
   530      *         text a&lt; x&amp;z t&gt;n &apos;g&apos; &quot;99&quot;&amp;&amp;&gt;&lt;<![CDATA[<geeting>]]>
       
   531      *     </element3>
       
   532      * </root>
       
   533      */
       
   534 
       
   535     // Create the writer
       
   536     CXmlWriter* writer = CXmlWriter::NewLC();
       
   537 
       
   538     // Write the prolog
       
   539     writer->AddPrologL(KVerNum);
       
   540     //writer->AddPrologL();
       
   541 
       
   542     // Write the root element start tag
       
   543     writer->AddStartTagL(KTagRoot);
       
   544     
       
   545     // Write an element with some text content
       
   546     writer->AddStartTagL(KTagElement1);
       
   547     writer->AddXmlTextL(KText);
       
   548     writer->AddEndTagL(KTagElement1);
       
   549 
       
   550     // Write an empty element with some attributes
       
   551     writer->OpenStartTagL(KTagElement2);
       
   552     writer->AddAttributeL(KAttr1, KVal1);
       
   553     writer->AddAttributeL(KAttr2, KVal2);
       
   554     writer->CloseStartTagL(ETrue);
       
   555 
       
   556     // Write an element with some attributes
       
   557     // and some text content
       
   558     writer->OpenStartTagL(KTagElement3);
       
   559     writer->AddAttributeL(KAttr1, KVal1);
       
   560     writer->CloseStartTagL(EFalse);
       
   561     writer->AddTextL(KText);
       
   562     writer->AddCdataSectL(KGreeting);
       
   563     writer->AddEndTagL(KTagElement3);
       
   564     
       
   565     // Write the root element end tag
       
   566     writer->AddEndTagL(KTagRoot);
       
   567 
       
   568     // Get the resulting XML document
       
   569     // and do something with it
       
   570     TPtrC8 doc = writer->DocPart(0, (writer->Length() - 1));
       
   571 
       
   572 
       
   573     /*
       
   574      * Create a new document using same writer object.
       
   575      * The new document looks like this (newlines
       
   576      * and indentation will no appear in the
       
   577      * actual output):
       
   578      *
       
   579      * <?xml version="1.0" encoding="ISO-8859-1"?>
       
   580      * <start>
       
   581      *     <ele1 attribute='value'>
       
   582      *         some text
       
   583      *     </ele1>
       
   584      *     <ele2 attr1='val1'/>
       
   585      * </start>
       
   586      */
       
   587 
       
   588 _LIT8(KTagStart, "start");
       
   589 _LIT8(KTagEle1, "ele1");
       
   590 _LIT8(KTagAttribute, "attribute");
       
   591 _LIT8(KTagValue, "value");
       
   592 _LIT8(KSomeText, "some text");
       
   593 _LIT8(KTagEle2, "ele2");
       
   594 
       
   595     // Reset writer
       
   596     writer->Reset();
       
   597 
       
   598     // Write the prolog
       
   599     writer->AddPrologL();
       
   600 
       
   601     // Write the root element start tag
       
   602     writer->AddStartTagL(KTagStart);
       
   603 
       
   604     // Write an element with attribute and some text content
       
   605     writer->AddStartTagL(KTagEle1);
       
   606     writer->AddAttributeL(KTagAttribute, KTagValue);
       
   607     writer->AddTextL(KSomeText);
       
   608     writer->AddEndTagL(KTagEle1);
       
   609 
       
   610     // Write an empty element with some attributes
       
   611     writer->OpenStartTagL(KTagEle2);
       
   612     writer->AddAttributeL(KAttr1, KVal1);
       
   613     writer->CloseStartTagL(ETrue);
       
   614 
       
   615     // Write the root element end tag
       
   616     writer->AddEndTagL(KTagStart);
       
   617 
       
   618     // Get the resulting XML document
       
   619     // and do something with it
       
   620     doc.Set(writer->DocPart(0, (writer->Length() - 1)));
       
   621 
       
   622     CleanupStack::PopAndDestroy(); // writer
       
   623     }
       
   624