examples/SysLibs/XmlExample/xmlexample.cpp

Go to the documentation of this file.
00001 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // License: "Symbian Foundation License v1.0" to Symbian Foundation
00004 // members and "Symbian Foundation End User License Agreement v1.0"
00005 // to non-members at the URL
00006 // "http://www.symbianfoundation.org/legal/licencesv10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // This example program demonstrates the use of XML Framework APIs. 
00015 // The code implements all the functions of MContentHandler class.
00016 // The example parses the XML file and WBXML file present in the specified path. To know how to 
00017 // write an XML file you can refer to this URL http://www.w3schools.com/xml/.
00018 //
00019 
00020 
00021 
00025 #include "xmlexample.h"
00026 #include <xml/documentparameters.h>
00027 #include <xml/parserfeature.h>
00028 #include <xml/matchdata.h>
00029 #include <e32cons.h>
00030 
00031 using namespace Xml;
00032 
00033 _LIT(KTitle, "XML example");
00034 _LIT(KStartKey, "\nThe example parses the given XML and WBXML files. Press any key to start\n");
00035 _LIT(KPressAKey,"\nPress any key to continue \n");
00036 _LIT(KExit,"\n Press any key to exit the application \n");
00037 _LIT(KParsingXml,"\nParsing the XML file");
00038 _LIT(KParsingWbXml,"\nParsing the WBXML file");
00039 _LIT(KParsingMatch,"\nParsing the XML file using match data criteria");
00040 _LIT(KError,"\n Could not open the XML file. Please check if the file exists.");
00041 
00042 _LIT(KOnStartDoc,"\n<!-- CXmlExample::OnStartDocumentL -->\n");
00043 _LIT(KOnEndDoc,"\n<!-- CXmlExample::OnEndDocumentL -->\n");
00044 _LIT(KOnContent,"<!-- CXmlExample::OnContentL -->");
00045 _LIT(KOnProcInstrn,"\n<!-- CXmlExample::OnProcessingInstructionL -->\n");
00046 _LIT(KOnError,"CXmlExample::OnError - ERROR: code=%d\n");
00047 
00048 _LIT(KVersion,"<?xml version=\"1.0\" encoding=\"%S\"?>\n");
00049 _LIT(KStartElement,"\nStart of element: <%S>\n");
00050 _LIT(KEndElement,"\nEnd of element: </%S>\n");
00051 _LIT(KContent,"%S\n");
00052 _LIT(KAttrib,"The attribute of element <%S>=\"%S\"");
00053 _LIT(KProcInstrn,"<?%S %S?>\n");
00054 _LIT(KOnSkipped, "Found skipped entity %S");
00055 _LIT(KNameSpaceRep,"\nNamespace mapping reporting enabled\n ");
00056 _LIT(KPrefixMap, "\nThe prefix is %S and URI is %S\n");
00057 _LIT(KPrefixEnd, "End of prefix URI mapping %S\n");
00058 
00059 _LIT(KSimpleXmlFile,    "z:\\private\\E80000AE\\xmlexample.xml");
00060 _LIT(KSimpleWbXmlFile,    "z:\\private\\E80000AE\\xmlexample.wbxml");
00061 _LIT8(KXmlMimeType, "text/xml");
00062 _LIT8(KWbxmlMimeType, "text/wbxml");
00063 
00068 CXmlExample* CXmlExample::NewL()
00069         {
00070         CXmlExample* self = new (ELeave) CXmlExample();
00071         CleanupStack::PushL(self);
00072         self->ConstructL();
00073         CleanupStack::Pop();
00074         return self;
00075         }
00076 
00077 CXmlExample::CXmlExample()
00078         :iError(KErrNone)
00079         {
00080         }
00081 
00082 void CXmlExample::ConstructL()
00083         {
00084         iConsole = Console::NewL(KTitle,TSize(KConsFullScreen,KConsFullScreen));
00085         iConsole->Printf ( KStartKey);
00086         iConsole->Getch ();
00087         }
00088 
00092 CXmlExample::~CXmlExample()
00093         {
00094         iConsole->Printf(KExit);
00095         iConsole->Getch();
00096         delete iConsole;
00097         }
00098 
00109 void CXmlExample::OnStartDocumentL(const RDocumentParameters& aDocParam, TInt aErrorCode)
00110         {
00111         User::LeaveIfError(aErrorCode);
00112         iConsole->Printf(KOnStartDoc);
00113 
00114         const TDesC8& encoding8 = aDocParam.CharacterSetName().DesC();
00115         HBufC* encoding16= Copy8To16LC(encoding8);
00116 
00117         iConsole->Printf(KVersion, encoding16);
00118         CleanupStack::PopAndDestroy(encoding16);
00119 
00120         }
00121         
00130 void CXmlExample::OnEndDocumentL(TInt aErrorCode)
00131         {
00132         User::LeaveIfError(aErrorCode);
00133         iConsole->Printf(KOnEndDoc);
00134         User::After(700000); // 0.7 seconds
00135         }
00136 
00148 void CXmlExample::OnStartElementL(const RTagInfo& aElement, const RAttributeArray& aAttributes, TInt aErrorCode)
00149         {
00150         User::LeaveIfError(aErrorCode);
00151 
00152         const TDesC8& localName8 = aElement.LocalName().DesC();
00153         
00154         HBufC* localName16 = Copy8To16LC(localName8);
00155         
00156         iConsole->Printf(KStartElement,localName16);
00157 
00158         CleanupStack::PopAndDestroy();  // Delete localName16
00159 
00160         TInt nAttributes = aAttributes.Count();
00161         for(TInt i=0; i<nAttributes; i++)
00162                 {
00163                 const RAttribute& attribute = aAttributes[i];
00164                 const RTagInfo& nameInfo = attribute.Attribute();
00165 
00166                 const TDesC8& localName8 = nameInfo.LocalName().DesC();
00167                 const TDesC8& value8 = attribute.Value().DesC();
00168 
00169                 HBufC* localName16 = Copy8To16LC(localName8);
00170                 HBufC* value16 = Copy8To16LC(value8);
00171 
00172                 iConsole->Printf(KAttrib, localName16, value16);
00173                 CleanupStack::PopAndDestroy(2,localName16);
00174                 }
00175         User::After(700000); // 0.7 sec
00176 
00177         }
00178 
00189 void CXmlExample::OnEndElementL(const RTagInfo& aElement, TInt aErrorCode)
00190         {
00191         User::LeaveIfError(aErrorCode);
00192         
00193         const TDesC8& localName8 = aElement.LocalName().DesC();
00194         HBufC* endBuf16 = Copy8To16LC(localName8);
00195         
00196         iConsole->Printf(KEndElement,endBuf16);
00197         
00198          CleanupStack::PopAndDestroy(endBuf16);
00199         }
00200 
00205 HBufC* CXmlExample::Copy8To16LC(const TDesC8& aDes)
00206         {
00207         HBufC* buf16 = HBufC::NewLC(aDes.Length());
00208         buf16->Des().Copy(aDes);
00209         return buf16; 
00210         }
00211 
00212 
00228 void CXmlExample::OnContentL(const TDesC8& aData8, TInt aErrorCode)
00229         {
00230         User::LeaveIfError(aErrorCode);
00231         iConsole->Printf(KOnContent);
00232         
00233         HBufC* data16 = Copy8To16LC(aData8);
00234         
00235         iConsole->Printf(KContent,data16);      
00236         
00237         CleanupStack::PopAndDestroy(data16);
00238         
00239         }
00240 
00252  void CXmlExample::OnProcessingInstructionL(const TDesC8& aTarget8, const TDesC8& aData8, TInt aErrorCode)
00253         {
00254         User::LeaveIfError(aErrorCode);
00255         iConsole->Printf(KOnProcInstrn);
00256 
00257 
00258         HBufC* target16 = Copy8To16LC(aTarget8);
00259         HBufC* data16 = Copy8To16LC(aData8);
00260 
00261         iConsole->Printf(KProcInstrn,target16,data16);
00262         CleanupStack::PopAndDestroy(2,target16);
00263         User::After(700000); // 0.7 sec
00264         }
00265 
00272 void CXmlExample::OnError(TInt aError)
00273         {
00274         iError = aError;
00275         iConsole->Printf(KOnError,aError);
00276         iConsole->Printf(KPressAKey);
00277         iConsole->Getch();
00278         }
00279 
00283 TAny* CXmlExample::GetExtendedInterface(const TInt32 /*aUid*/)
00284         {
00285         // do nothing.
00286         return NULL;
00287         }
00288         
00300 void CXmlExample::OnStartPrefixMappingL(const RString& aPrefix, const RString& aUri, TInt aErrorCode)
00301         {
00302         User::LeaveIfError(aErrorCode);
00303         const TDesC8& localPrefix8 = aPrefix.DesC();
00304         HBufC* temp1 = Copy8To16LC(localPrefix8);
00305         const TDesC8& localUri8 = aUri.DesC();
00306         HBufC* temp2 = Copy8To16LC(localUri8);
00307         
00308         iConsole->Printf(KPrefixMap, temp1, temp2);
00309         CleanupStack::PopAndDestroy(2, temp1);
00310         
00311         iConsole->Printf(KPressAKey);
00312         iConsole->Getch();
00313                 
00314         }
00315         
00319 void CXmlExample::OnEndPrefixMappingL(const RString& aPrefix, TInt aErrorCode)
00320         {
00321         User::LeaveIfError(aErrorCode);
00322         const TDesC8& localPrefix8 = aPrefix.DesC();
00323         HBufC* temp = Copy8To16LC(localPrefix8);
00324         iConsole->Printf(KPrefixEnd, temp);
00325         CleanupStack::PopAndDestroy(temp);
00326         }
00327 
00331 void CXmlExample::OnIgnorableWhiteSpaceL(const TDesC8&, TInt)
00332         {
00333         
00334         }
00335 
00347 void CXmlExample::OnSkippedEntityL(const RString& aName, TInt aErrorCode)
00348         {
00349         User::LeaveIfError(aErrorCode);
00350         
00351         const TDesC8& localName8 = aName.DesC();
00352         HBufC* temp = Copy8To16LC(localName8);
00353         
00354         iConsole->Printf(KOnSkipped, temp);
00355         CleanupStack::PopAndDestroy(temp);
00356         User::After(700000); // 0.7 sec delay           
00357         }
00358 
00362 void CXmlExample::ParseExampleDocumentsL()
00363         {
00364         RFs fs;
00365         User::LeaveIfError(fs.Connect());
00366         CleanupClosePushL(fs);
00367 
00368         RFile handle;
00369         TInt err;
00370         // Open an example XML file for parsing
00371         TRAP(err,handle.Open(fs, KSimpleXmlFile, EFileShareReadersOnly));
00372         if (err != KErrNone)
00373                 {
00374                 iConsole->Printf(KError);
00375                 User::Leave(err);
00376                 }
00377         CleanupClosePushL(handle);
00378 
00379         // Open parser session for text/xml mime type
00380         CParser* parser = CParser::NewLC(KXmlMimeType, *this);
00381         
00382         iConsole->Printf(KParsingXml);
00383         iConsole->Printf(KPressAKey);
00384         iConsole->Getch();
00385         
00386         err = parser->EnableFeature(EReportNamespaceMapping);
00387          
00388         if(err==KErrNone) 
00389         {
00390         iConsole->Printf(KNameSpaceRep);
00391         } 
00392 
00393         // Starts parser session
00394         ParseL(*parser, handle); // this will result in callbacks to handler
00395         
00396         // Closes the parser session.
00397         CleanupStack::PopAndDestroy(parser);
00398         CleanupStack::Pop(&handle);
00399 
00400         // Open an example WBXML file for parsing
00401         TRAP(err,handle.Open(fs, KSimpleWbXmlFile, EFileShareReadersOnly));
00402         if (err != KErrNone)
00403                 {
00404                 iConsole->Printf(KError);
00405                 User::Leave(err);
00406                 }
00407         CleanupClosePushL(handle);
00408 
00409         parser = CParser::NewLC(KWbxmlMimeType, *this);
00410         
00411         iConsole->Printf(KParsingWbXml);
00412         iConsole->Printf(KPressAKey);
00413         iConsole->Getch();
00414         
00415         // Starts parser session and active scheduler.
00416         ParseL(*parser, handle); // this will result in callbacks to handler
00417 
00418         CleanupStack::PopAndDestroy(parser);
00419         CleanupStack::Pop(&handle);
00420 
00421         // Open an example XML file for parsing using a parser specified with 
00422         // MatchData. In this case, the default Symbian platform parser is requested.
00423         
00424         TRAP(err,handle.Open(fs, KSimpleXmlFile, EFileShareReadersOnly));
00425         if (err != KErrNone)
00426                 {
00427                 iConsole->Printf(KError);
00428                 User::Leave(err);
00429                 }       
00430         CleanupClosePushL(handle);
00431 
00432         CMatchData *matchData = CMatchData::NewLC();
00433         matchData->SetMimeTypeL(KXmlMimeType);
00434         matchData->SetVariantL(_L8("Symbian"));
00435         
00436         iConsole->Printf(KParsingMatch);
00437         iConsole->Printf(KPressAKey);
00438         iConsole->Getch();
00439         
00440         parser = CParser::NewLC(*matchData, *this);
00441         ParseL(*parser, handle);
00442         
00443         CleanupStack::PopAndDestroy(2,matchData);
00444         CleanupStack::PopAndDestroy(&handle);
00445         CleanupStack::PopAndDestroy(&fs);
00446         
00447         }
00448 
00449 LOCAL_C void MainL()
00450         {
00451         CXmlExample* app = CXmlExample::NewL();
00452         CleanupStack::PushL(app);
00453         
00454         app->ParseExampleDocumentsL();
00455         CleanupStack::PopAndDestroy(app);
00456 
00457         }
00458 
00459 GLDEF_C TInt E32Main()
00460         {
00461     __UHEAP_MARK;
00462     CTrapCleanup* cleanup = CTrapCleanup::New();
00463     if(cleanup == NULL)
00464         {
00465         return KErrNoMemory;
00466         }
00467     TRAPD(err, MainL());
00468         if(err != KErrNone)
00469                 {
00470                 User::Panic(_L("Failed to complete"),err);
00471                 }
00472 
00473     delete cleanup;
00474     __UHEAP_MARKEND;
00475     return KErrNone;
00476         }
00477 
00478 
00479 

Generated by  doxygen 1.6.2