Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Symbian XML parsing plugins

From Wiki
Jump to: navigation, search


CParser default plugin - Expat

CParser class is using by default Expat parser. It is stream based let's say SAX-like parser. Symbian implementation of the Expat XML parser plugin is a little bit old (S^3 has version 1.95.5 from 2002 year) and it has one known problem - it wrongly parses XML attributes of size larger tham 12kB. Elements with such large attributes are notified to the application as a content of the parent element and also they missing first ~12kB of data.

Source code of the Expat parser plugin can be found in Symbian Foundation source code: \PDK_4.0.a\\\sf\os\xmlsrv\xml\xmlexpatparser\
Symbian Foundation source code is available on SourceForge prtal:

CParser Libxml2 plugin

To resolve problem with large attributes and parse XML by CParse class, we can change XML parsing plugin from Expat to Libxml2. It is available at least from S^3 (tested on Nokia E7). To select parsing plugin we are passing it during construction of CParse class:

_LIT8(KMimeType, "text/xml");
_LIT8(KParser, "libxml2");
iParserPlugin = CMatchData::NewL();
iParserPlugin->SetMimeTypeL( KMimeType() );
iParserPlugin->SetVariantL( KParser() );
iParser = CParser::NewL( *iParserPlugin, *this ); // assuming the this class implements MContentHandler interface

This will resolve large attribute problem without need of moving to DOM parser and with only little code change.
Remember to destroy CMatchData in your class destructor.

Source code of the Libxml2 parser plugin can be found also in Symbian Foundation source code: \PDK_4.0.a\\\sf\os\xmlsrv\xml\xmllibxml2parser\

CParser optimization using string table

Using SAX parsing element and attributes resolve requires comparison of the element and attributes names. To minimize string comparisons count we can pass predefined string table to the CParser object and get elements and attributes indexes in our table.

Firstly we need to declare our string table:

#include <stringpool.h> 
_LIT8(KParserStr_1, "element1");
_LIT8(KParserStr_2, "element2");
_LIT8(KParserStr_3, "attr1");
_LIT8(KParserStr_4, "attr2");
const void * const KParserStringPointers[] =
(const void*)&KParserStr_1,
(const void*)&KParserStr_2,
(const void*)&KParserStr_3,
(const void*)&KParserStr_4
const TStringTable KParserStringTable = { 4, KParserStringPointers, ETrue }; // to use it with CParser case sensitive must be set to ETrue
enum TParserStringTable

Now we need to load the table to the parser. We can do it just before starting of parsing:

iParser->StringPool().OpenL( KParserStringTable );

Now in callbacks we can resolve element and attribute string names just by enumeration:

void CMyParser::OnStartElementL(const RTagInfo& aElement, const RAttributeArray& aAttributes,TInt aErrorCode)
TInt idx = aElement.LocalName().Index( KParserStringTable );
switch ( idx )
case EParserString_element1:
ParseElement( aAttributes );
case EParserString_element1:
ParseElement( aAttributes );
void CMyParser::ParseElement(const RAttributeArray& aAttributes)
for ( TInt i = 0; i < aAttributes.Count(); i++ )
TInt idx = aAttributes[ i ].Attribute().LocalName().Index( KParserStringTable );
switch ( idx )
case EParserString_attr1:
case EParserString_attr2:

Article Metadata
Platform(s): Symbian^1 and later
Nokia Belle
Symbian Anna
S60 5th Edition
Keywords: XML, SAX, Expat, libxml2, DOM
Created: mstrug (24 Jan 2013)
Last edited: hamishwillee (30 May 2013)

Version Hint

Windows Phone: [[Category:Windows Phone]]
[[Category:Windows Phone 7.5]]
[[Category:Windows Phone 8]]

Nokia Asha: [[Category:Nokia Asha]]
[[Category:Nokia Asha Platform 1.0]]

Series 40: [[Category:Series 40]]
[[Category:Series 40 1st Edition]] [[Category:Series 40 2nd Edition]]
[[Category:Series 40 3rd Edition (initial release)]] [[Category:Series 40 3rd Edition FP1]] [[Category:Series 40 3rd Edition FP2]]
[[Category:Series 40 5th Edition (initial release)]] [[Category:Series 40 5th Edition FP1]]
[[Category:Series 40 6th Edition (initial release)]] [[Category:Series 40 6th Edition FP1]] [[Category:Series 40 Developer Platform 1.0]] [[Category:Series 40 Developer Platform 1.1]] [[Category:Series 40 Developer Platform 2.0]]

Symbian: [[Category:Symbian]]
[[Category:S60 1st Edition]] [[Category:S60 2nd Edition (initial release)]] [[Category:S60 2nd Edition FP1]] [[Category:S60 2nd Edition FP2]] [[Category:S60 2nd Edition FP3]]
[[Category:S60 3rd Edition (initial release)]] [[Category:S60 3rd Edition FP1]] [[Category:S60 3rd Edition FP2]]
[[Category:S60 5th Edition]]
[[Category:Symbian^3]] [[Category:Symbian Anna]] [[Category:Nokia Belle]]

This page was last modified on 30 May 2013, at 04:36.
39 page views in the last 30 days.