Symbian提供了CParser这个类来供我们解析XML文档。
首先我们解析XML文档的类要继承MContentHandler
实现它内部的一些虚函数
void OnStartDocumentL(const RDocumentParameters &aDocParam,
TInt aErrorCode);//当我们开始解析的时候就会调用他了
void OnEndDocumentL(TInt aErrorCode);
void OnStartElementL(const RTagInfo &aElement,
const RAttributeArray &aAttributes, TInt aErrorCode);//然后会调用这个函数(回调)RTagInfo中包涵了XML文档中的TAG信息(const TDesC8& elementName = aElement.LocalName().DesC();)可以取出TAG的内容,然后我们给特定的内容设置上标志位。
void OnEndElementL(const RTagInfo &aElement, TInt aErrorCode);
void OnContentL(const TDesC8 &aBytes, TInt aErrorCode);//这里的就是我们要取得的内容了,我们根据上面的标志位来取得特定的内容,内容就放在了aBytes中
void OnStartPrefixMappingL(const RString &aPrefix, const RString &aUri,
TInt aErrorCode);
void OnEndPrefixMappingL(const RString &aPrefix, TInt aErrorCode);
void OnIgnorableWhiteSpaceL(const TDesC8 &aBytes, TInt aErrorCode);
void OnSkippedEntityL(const RString &aName, TInt aErrorCode);
void OnProcessingInstructionL(const TDesC8 &aTarget, const TDesC8 &aData,
TInt aErrorCode);
void OnError(TInt aErrorCode);
TAny *GetExtendedInterface(const TInt32 aUid);
其他的一些函数根据个人需要去实现吧!
另在iParser.Parser()后会回调OnStartElementL()然后会一直在那里循环回调,直到解析完毕整个XML文档,XML文档中的所有内容是放到一个描述符中的
下面是一个示例代码:
#ifndef SETTINGXMLPASER_H_
#define SETTINGXMLPASER_H_
#include <e32base.h>
#include <f32file.h>
#include <xml/contenthandler.h>
#include <xml/parser.h>
#include <setting.h>
using namespace Xml ;
class CSettingXMLParser : public MContentHandler
{
public:
static CSettingXMLParser* NewLC(CSetting& aSetting) ;
static CSettingXMLParser* NewL(CSetting& aSetting) ;
~CSettingXMLParser() ;
public:
void StartParsingL(const TDesC& aFileName);
private: //from MContentHandler
void OnStartDocumentL(const RDocumentParameters &aDocParam,
TInt aErrorCode);
void OnEndDocumentL(TInt aErrorCode);
void OnStartElementL(const RTagInfo &aElement,
const RAttributeArray &aAttributes, TInt aErrorCode);
void OnEndElementL(const RTagInfo &aElement, TInt aErrorCode);
void OnContentL(const TDesC8 &aBytes, TInt aErrorCode);
void OnStartPrefixMappingL(const RString &aPrefix, const RString &aUri,
TInt aErrorCode);
void OnEndPrefixMappingL(const RString &aPrefix, TInt aErrorCode);
void OnIgnorableWhiteSpaceL(const TDesC8 &aBytes, TInt aErrorCode);
void OnSkippedEntityL(const RString &aName, TInt aErrorCode);
void OnProcessingInstructionL(const TDesC8 &aTarget, const TDesC8 &aData,
TInt aErrorCode);
void OnError(TInt aErrorCode);
TAny *GetExtendedInterface(const TInt32 aUid);
private:
CSettingXMLParser(CSetting& aSetting) ;
void ConstructL() ;
private:
CParser* iParser;
HBufC8* iBuffer;
RFile iFile;
CSetting& iSetting ;
TInt iState ;
} ;
#endif /* SETTINGXMLPASER_H_ */
#include "settingXMLParser.h"
#include <e32cons.h>
#include <coemain.h>
#include <settingManager.h>
_LIT8( KXmlMimeType, "text/xml" );
_LIT8( KIap , "iap" ) ;
_LIT8( KSwPath , "swpath" ) ;
_LIT8( KFilePath , "filepath" ) ;
CSettingXMLParser::CSettingXMLParser(CSetting& aSetting):iState(0) , iSetting(aSetting)
{
}
void CSettingXMLParser::ConstructL()
{
iBuffer = NULL;
iParser = CParser::NewL(KXmlMimeType, *this);
}
CSettingXMLParser* CSettingXMLParser::NewLC(CSetting& aSetting)
{
CSettingXMLParser* self = new (ELeave) CSettingXMLParser(aSetting);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CSettingXMLParser* CSettingXMLParser::NewL(CSetting& aSetting)
{
CSettingXMLParser* self = CSettingXMLParser::NewLC(aSetting);
CleanupStack::Pop(self);
return self;
}
CSettingXMLParser::~CSettingXMLParser()
{
delete iBuffer;
delete iParser;
iFile.Close() ;
}
void CSettingXMLParser::StartParsingL(const TDesC& aFileName)
{
RFs &fs = CCoeEnv::Static()->FsSession() ;
TInt err = iFile.Open(fs , aFileName , EFileRead);
if(err == KErrNotFound) //如果是首次进入先创建XML文档并初始化
{
CSettingManager *manager = CSettingManager::NewLC() ;
CSetting *setting = CSetting::NewLC() ;
manager->SaveL(setting) ;
CleanupStack::PopAndDestroy(setting) ;
CleanupStack::PopAndDestroy(manager) ;
err = iFile.Open(fs , aFileName,EFileRead);
User::LeaveIfError(err) ;
}
TInt size;
User::LeaveIfError(iFile.Size(size));
delete iBuffer;
iBuffer = 0;
iBuffer = HBufC8::NewL(size);
TPtr8 bufferPtr(iBuffer->Des());
User::LeaveIfError(iFile.Read(bufferPtr));
// Now, we have the whole file content in iBuffer.
// We are ready to parse the XML content.
iParser->ParseBeginL();
iParser->ParseL(*iBuffer);
//TRAPD(err, iParser->ParseL(*iBuffer)) ;
// Since we read the whole file contents within one-shot,
// we can call ParseEndL() right after calling ParseL().
iParser->ParseEndL();
}
void CSettingXMLParser::OnStartDocumentL(const RDocumentParameters &aDocParam,
TInt aErrorCode)
{
if (KErrNone == aErrorCode)
{
}
}
void CSettingXMLParser::OnEndDocumentL(TInt aErrorCode)
{
if (KErrNone == aErrorCode)
{
}
}
void CSettingXMLParser::OnStartElementL(const RTagInfo &aElement,
const RAttributeArray &aAttributes, TInt aErrorCode)
{
if (KErrNone == aErrorCode)
{
const TDesC8& elementName = aElement.LocalName().DesC();
if(elementName.Compare(KIap()) == 0)
{
iState = 0 ;
}
else if(elementName.Compare(KSwPath()) == 0)
{
iState = 1 ;
}
else if(elementName.Compare(KFilePath()) == 0)
{
iState = 2 ;
}
}
}
void CSettingXMLParser::OnEndElementL(const RTagInfo &aElement, TInt aErrorCode)
{
}
void CSettingXMLParser::OnContentL(const TDesC8 &aBytes, TInt aErrorCode)
{
TLex8 lex(aBytes) ;
TInt value = 0 ;
TInt err = lex.Val(value);
//User::LeaveIfError(lex.Val(value)) ;
switch(iState)
{
case 0:
iSetting.SetIap(value) ;
break ;
case 1:
iSetting.SetSwPath(value) ;
break ;
case 2:
iSetting.SetFilePath(value) ;
break ;
default:
break ;
}
}
void CSettingXMLParser::OnStartPrefixMappingL(const RString &aPrefix,
const RString &aUri, TInt aErrorCode)
{
}
void CSettingXMLParser::OnEndPrefixMappingL(const RString &aPrefix,
TInt aErrorCode)
{
}
void CSettingXMLParser::OnIgnorableWhiteSpaceL(const TDesC8 &aBytes,
TInt aErrorCode)
{
}
void CSettingXMLParser::OnSkippedEntityL(const RString &aName, TInt aErrorCode)
{
}
void CSettingXMLParser::OnProcessingInstructionL(const TDesC8 &aTarget,
const TDesC8 &aData, TInt aErrorCode)
{
}
void CSettingXMLParser::OnError(TInt aErrorCode)
{
}
TAny* CSettingXMLParser::GetExtendedInterface(const TInt32 aUid)
{
}