×
Namespaces

Variants
Actions

Archived:Syexpat XML parser for S60 2nd edition

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

S60 2nd Edition and earlier are no longer relevant platforms. The links provided here are also broken so this article is becoming unusable (not deleting as the resources may be available elsewhere).

Article Metadata
CompatibilityArticle
Created: giridharn (17 May 2007)
Last edited: hamishwillee (18 Jun 2013)

Here is the detailed description on using syexpat xml parser in your applications.

  • Step 1: Download the SyExpat build from [--hamishwillee : Link broken - was formerly at:] www.toddsoftware.com/index.php?option=com_docman&Itemid=93&task=view_category&catid=105&order=dmname&ascdesc=ASC
  • Step 2: Once you have downloaded the SyExpat, build it for WINS/WINSW/ARMI. And include this given XmlHelper.cpp and Xmlhelper.h in your application.
  • Step 3: Use this code in your Container/AppUi class from where you want to call the parser.


Code to use call parser

///INCLUDE these files
 
#include "SyExpat.h"
#include "xmlhelper.h"
 
 
// 1. try to open the file containing the xml and if it
// was opened successfully put it on the cleanup stack
// with a close
 
 
//Here XmlFile is the filename of the xml ,which you want to parse/
 
RFile file;
User::LeaveIfError(file.Open(fs, XmlFile, EFileStream));
CleanupClosePushL(file);
 
// 2. Create the XML Parser and set the default document handlers
 
CSyContextualParser* reader = CSyContextualParser::NewLC(TPtrC());
 
CDocumentHandler *docHandler = new CDocumentHandler();
docHandler->SetContainer(this);
reader->PushDocHandler(docHandler);
 
CDeclarationHandler *declHandler = new CDeclarationHandler();
reader->PushDeclHandler(declHandler);
 
// 3. We set up the buffer with a default size of 128 bytes.
// This buffer is set purposly small so that we execute the
// parse stuff as often as possible to show aynchronous parsing
// We alse set up some loop variables
 
const int KMaxBufLen = 128;
TBuf8<KMaxBufLen> buf;
TBool isFinal = ETrue;
TSyExpatStatus status = EStatusOk;
TInt i = 0;
do
{
// 4. First read some data
// Note that if there was an error reading there
// data an error will be returned, this is not the
// case with eof where the buffer will be smaller than
// maxBufLen.
 
User::LeaveIfError(file.Read(buf, KMaxBufLen));
 
// 5. Next we attempt to parse the result buffer
// or if we reached the end of the file the
// partial buffer
 
isFinal = (buf.Length() != KMaxBufLen);
status = reader->Parse(buf, isFinal);
i++;
 
// 6. When the parser returns an error
// we log it
// Otherwise if there is no more data
// then we report we have finished
 
if (status != EStatusOk)
{
_LIT(KError,"Error on Line %d Column %d\n");
 
//test.Printf(KError, reader->GetCurrentLineNumber(),
// reader->GetCurrentColumnNumber());
 
}
else
{
if (isFinal)
{
_LIT(KFinished, "Finished parsing file\n");
}
}
 
// 7. finally we see if we have to loop round
// any more - we stop if we find an error or
// we have reached the end of the file
 
} while (status == EStatusOk && !isFinal);
 
// 8. Finally delete the parser
// and close the file handle
 
CleanupStack::PopAndDestroy(2);
 
 
delete declHandler;
declHandler = NULL;
 
delete docHandler;
docHandler = NULL;


XmlHelper.h

#ifndef __XML_HELPER_H
#define __XML_HELPER_H
 
class CDocumentHandler : public CBase, public MSyDocHandler
{
public:
CDocumentHandler(RTest& aTest);
virtual ~CDocumentHandler();
 
virtual void StartElement(const TQualified& aName,
const RArray<TAttribute>& attributes);
virtual void EndElement(const TQualified& aName);
virtual void CharacterData(const TDesC& aData);
virtual void ProcessingInstruction(const TDesC& aTarget,
const TDesC& aData);
virtual void Comment(const TDesC& aData);
virtual void StartCDataSection();
virtual void EndCDataSection();
virtual void Default(const TDesC& /*aData*/);
 
virtual void SetParser(CExpatParserBase* aParser);
private:
void Indent(const TInt aDepth);
 
protected:
RTest& iTest;
TInt iDepth;
};
 
class CDeclarationHandler : public CBase, public MSyDeclHandler
{
public:
CDeclarationHandler(RTest& aTest);
virtual ~CDeclarationHandler();
 
virtual TSyExpatStatus NotStandalone();
virtual void StartDocType( const TDesC& aDocType,
const TDesC& aSystemId, const TDesC& aPublicId,
const TBool aHasInternalSubset);
 
virtual void EndDocType();
virtual void StartNamespace(const TDesC& aPrefix,
const TDesC& aURI);
virtual void EndNamespace(const TDesC& aPrefix);
virtual void Entity(const TDesC& aName,
const TBool aIsParameter,
const TDesC& aValue, const TDesC& aBase,
const TDesC& aSystemId, const TDesC& aPublicId,
const TDesC& aNotation);
virtual void SkippedEntity(const TDesC& aEntity,
const TBool aIsParameter);
virtual void Notation(const TDesC& aNotationName,
const TDesC& aBase,
const TDesC& aSystemId, const TDesC& aPublicId);
virtual void SetParser(CExpatParserBase* aParser);
 
private:
void Indent(const TInt aDepth);
 
protected:
RTest& iTest;
TInt iDepth;
};
 
#endif // __XML_HELPER_H


XmlHelper.cpp

#include "SyExpat.h"
#include "e32test.h"
#include "XMLHelper.h"
 
CDocumentHandler::CDocumentHandler(RTest& aTest)
: iTest(aTest),
iDepth(0)
{
}
 
CDocumentHandler::~CDocumentHandler()
{
}
 
void CDocumentHandler::Indent(const TInt aDepth)
{
for (TInt i=0; i < aDepth; i++)
{
// _LIT(KTab, " ");
// iTest.Printf(KTab);
}
}
 
void CDocumentHandler::StartElement(const TQualified& aName,
const RArray<TAttribute>& attributes)
{
 
TPtrC elemName(aName.iLocalName);
TPtrC elemPrefix(aName.iPrefix);
TPtrC elemURI(aName.iURI);
 
if (elemPrefix.Length() == 0)
{
// locally named element
_LIT(KElementFormatString,"LN:%S ");
Indent(iDepth);iTest.Printf(KElementFormatString, &elemName);
}
else
{
if (elemURI.Length() > 0)
{
// namespace declaration for URI
_LIT(KNamespaceFormatString,"ELEM:%S ");
Indent(iDepth); iTest.Printf(
KNamespaceFormatString, &elemURI);
}
 
 
// Fully qualified element
_LIT(KElementFormatString, ":%S:%S ");
Indent(iDepth);iTest.Printf(
KElementFormatString, &elemPrefix, &elemName);
}
 
TInt i = 0;
const TInt attrCount = attributes.Count() /2;
for (i = 0; i < attrCount; i++)
{
const TAttribute& attribute = attributes[i];
const TPtrC attrPrefix(attribute.iName.iPrefix);
 
//This is attribute tag Name //
const TPtrC attrName(attribute.iName.iLocalName);
 
//This is attribute value //[[Category:How To]]
const TPtrC attrValue(attribute.iValue);
 
 
if (attrPrefix.Length() == 0)
{
// Locally Named attribute
_LIT(KAttributeFormatString, "LNA:%S = %S\n");
Indent(iDepth);iTest.Printf(
KAttributeFormatString, &attrName, &attrValue);
}
else
{
// Fully Qualified attribute
_LIT(KAttributeFormatString, "QNA:%S:%S = %S\n");
Indent(iDepth);iTest.Printf(KAttributeFormatString,
&attrPrefix, &attrName, &attrValue);
 
}
}
 
if (i == 0)
{
_LIT(KNewLine, "\n");
iTest.Printf(KNewLine);
}
 
iDepth++;
}
 
void CDocumentHandler::EndElement(const TQualified& /*aName*/)
{
// _LIT(KElementFormatString,"\n");
// Indent(iDepth);iTest.Printf(KElementFormatString);
}
 
void CDocumentHandler::CharacterData(const TDesC& aData)
{
 
/////////Here you get the value-fileds of your xml file
// _LIT(KElementFormatString,"%S");
// TPtrC tmp(aData);
// Indent(iDepth);iTest.Printf(KElementFormatString, &tmp);
}
 
void CDocumentHandler::ProcessingInstruction(const TDesC& aTarget,
const TDesC& aData)
{
_LIT(KElementFormatString,"PI %S = %S\n");
 
TPtrC target(aTarget);
TPtrC data(aData);
Indent(iDepth);iTest.Printf(KElementFormatString, &target, &data);
}
 
void CDocumentHandler::Comment(const TDesC& aData)
{
_LIT(KElementFormatString,"Comment - %S\n");
 
TPtrC tmp(aData);
Indent(iDepth); iTest.Printf(KElementFormatString, &tmp);
}
 
void CDocumentHandler::StartCDataSection()
{
_LIT(KDefaultFormatString,"CDATA Start\n");
Indent(iDepth); iTest.Printf(KDefaultFormatString);
}
 
void CDocumentHandler::EndCDataSection()
{
_LIT(KDefaultFormatString,"CDATA End\n");
Indent(iDepth); iTest.Printf(KDefaultFormatString);
}
 
void CDocumentHandler::Default(const TDesC& aData)
{
// _LIT(KDefaultFormatString,"Default - S\n");
// TPtrC tmp(aData);
// Indent(iDepth); iTest.Printf(KDefaultFormatString, &tmp);
}
 
void CDocumentHandler::SetParser(CExpatParserBase* /*aParser*/)
{
// Does nothing
}
 
////////////////////////////////////////////////////////////////
CDeclarationHandler::CDeclarationHandler(RTest& aTest)
: iTest(aTest),
iDepth(0)
{
}
 
CDeclarationHandler::~CDeclarationHandler()
{
}
 
void CDeclarationHandler::Indent(const TInt aDepth)
{
for (TInt i=0; i < aDepth; i++)
{
_LIT(KTab, " ");
// iTest.Printf(KTab);
}
}
 
 
TSyExpatStatus CDeclarationHandler::NotStandalone()
{
_LIT(KDefaultFormatString,"This document is not standalone!\n");
Indent(iDepth); iTest.Printf(KDefaultFormatString);
 
return EStatusOk;
}
 
void CDeclarationHandler::StartDocType( const TDesC& aDocType,
const TDesC& /*aSystemId*/, const TDesC& /*aPublicId*/,
const TBool /*aHasInternalSubset*/)
{
_LIT(KDefaultFormatString,"DT: %S\n");
 
TPtrC doctype(aDocType);
Indent(iDepth); iTest.Printf(KDefaultFormatString, &doctype);
iDepth++;
 
}
 
void CDeclarationHandler::EndDocType()
{
iDepth--;
}
 
void CDeclarationHandler::StartNamespace(const TDesC& aPrefix,
const TDesC& aURI)
{
_LIT(KDefaultFormatString,"NS: %S:%S\n");
 
TPtrC prefix(aPrefix);
TPtrC uri(aURI);
 
Indent(iDepth); iTest.Printf(KDefaultFormatString, &prefix, &uri);
 
iDepth++;
}
 
void CDeclarationHandler::EndNamespace(const TDesC& /*aPrefix*/)
{
iDepth--;
}
 
void CDeclarationHandler::Entity(const TDesC& aName,
const TBool /*aIsParameter*/, const TDesC& aValue,
const TDesC& /*aBase*/, const TDesC& /*aSystemId*/,
const TDesC& /*aPublicId*/, const TDesC& /*aNotation*/)
{
_LIT(KDefaultFormatString,"ENT: %S = %S\n");
 
TPtrC name(aName);
TPtrC value(aValue);
 
Indent(iDepth); iTest.Printf(KDefaultFormatString, &name, &value);
 
}
 
void CDeclarationHandler::SkippedEntity(const TDesC& aEntity,
const TBool /*aIsParameter*/)
{
_LIT(KDefaultFormatString,"SE: %S\n");
 
TPtrC entity(aEntity);
 
Indent(iDepth); iTest.Printf(KDefaultFormatString, &entity);
 
}
 
void CDeclarationHandler::Notation( const TDesC& aNotationName,
const TDesC& /*aBase*/, const TDesC& /*aSystemId*/,
const TDesC& /*aPublicId*/)
{
_LIT(KDefaultFormatString,"NOT: %S\n");
 
TPtrC notationName(aNotationName);
 
Indent(iDepth); iTest.Printf(KDefaultFormatString, &notationName);
}
 
void CDeclarationHandler::SetParser(CExpatParserBase* /*aParser*/)
{
// Does nothing
}
  • Step 4: Dont forget to use syexpat.dll of ARMI/THUMB build in your package file like this:
"syexpat.dll" -"!:\system\libs\syexpat.dll"
  • Step 5: Say thanks to Toddsoftware (www.toddsoftware.com), who has given us SyExpat xml parser for our application development.
This page was last modified on 18 June 2013, at 08:01.
55 page views in the last 30 days.
×