接上回,这篇介绍那个MContentHandler的实现,这是SAX解析方法的核心所在。
先看看我要解析的XML文件如下所示,其实很简单,因为它除了Element和Attribute以外没有其它东西了。
我们的解析处理器的声明如下:
#include
#include
using namespace Xml;
class TNewsChannel
...{
public:
TInt id;
HBufC16 * title;
};
class TNewsContent
...{
public:
TInt id;
TInt pid;
HBufC16 * title;
};
class CChannelXmlHandler : public MContentHandler ...{
public:
// Constructors and destructor
~CChannelXmlHandler();
static CChannelXmlHandler* NewL();
static CChannelXmlHandler* NewLC();
RArray* GetChannels();
RArray* GetContents();
TInt GetContent(TInt pid,TInt index);
TInt ContentCount(TInt pid);
private:
CChannelXmlHandler();
void ConstructL();
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 );
// ... ...
private:
TInt iCurPID;
RArray iChannels;
RArray iContents;
};
大多数是MContentHandler所声明的方法,这就是SAX事件解析模式的关键了,我们只需要在这些方法中做相应的处理即可。
除此之外,iChannels和iContents是我们定义了用来保存解析结果的成员,它的类型是RArray,关于RArray可以参考我的别一篇笔记:http://blog.csdn.net/sharetop/archive/2008/03/21/2203450.aspx
因为我们的XML比较简单,所以在CPP中只要处理OnStartElementL就可以了:
这个回调会在解析器遇到元素头时进入,然后我们就可以根据传入的参数取到当前元素的信息,如元素名称、属性值等,将它们保存在我们定义的数据成员中以备将来使用即可。
在使用这个解析器的地方,比如我们的AppView负责解析XML文件,那它应该包含一个MContentHandler的成员,并且它实现接口MXMLHandlerObserver。
于是,这样启动解析过程:
iChannelHandler=CChannelXmlHandler::NewL();
iXmlParser=CXMLActiveParser::NewL(*this,*iChannelHandler);
iXmlParser->StartL(KChannelXMLFile);
然后在它的OnParseCompleted方法中去iChannelHandler中取出解析结果,展示出来或者随便怎么用了。
void CChannelXmlHandler::OnStartElementL( const Xml::RTagInfo &aElement,
const Xml::RAttributeArray &aAttributes, TInt aErrorCode )
...{
if(aElement.LocalName().DesC().Compare(KChannelName)==0)...{
TNewsChannel chn;
for(TInt i=0;i if(aAttributes.Attribute().LocalName().DesC().Compare(KTitleName)==0)...{
chn.title=CnvUtfConverter::ConvertToUnicodeFromUtf8L(aAttributes.Value().DesC());
}
else if(aAttributes.Attribute().LocalName().DesC().Compare(KIdName)==0)...{
TLex8 lex;
lex.Assign(aAttributes.Value().DesC());
lex.Val(chn.id);
}
}
iChannels.Append(chn);
iCurPID=chn.id;
}
else if(aElement.LocalName().DesC().Compare(KContentName)==0)...{
TNewsContent cnt;
cnt.pid=iCurPID;
for(TInt i=0;i if(aAttributes.Attribute().LocalName().DesC().Compare(KIdName)==0)...{
TLex8 lex;
lex.Assign(aAttributes.Value().DesC());
lex.Val(cnt.id);
}
else if(aAttributes.Attribute().LocalName().DesC().Compare(KTitleName)==0)...{
cnt.title=CnvUtfConverter::ConvertToUnicodeFromUtf8L(aAttributes.Value().DesC());
}
}
iContents.Append(cnt);
}
【编辑推荐】
- Symbian每6个月发布一款新开源操作系统
- 微软建WindowsMarketplace 完善智能机平台
- 大力模仿Android 传微软WM6.5可装插件