Symbian学习笔记(16) - 解析XML文件(下)
2008-04-23 16:55
531 查看
继续,刚才发表时出错,打开一看是别的人文章了!又白写了,晕……刚才的思路没了,重新来过吧。
书接上回,这篇介绍那个MContentHandler的实现,这是SAX解析方法的核心所在。
先看看我要解析的XML文件如下所示,其实很简单,因为它除了Element和Attribute以外没有其它东西了。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="utf-8" ?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<channels>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<channel id="10" title="时政" >
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1001" title="广东牛奶中毒事件污染源调查结果1周后公布"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1002" title="河南淅川公安局因儿童被拐案设'局耻日'"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1003" title="深圳大学135名师生感染病毒引发腹泻"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</channel>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<channel id="11" title="国际">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1101" title="巴以将于4月7日恢复领导人级和谈"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1102" title="古巴解除长期禁令允许国民入住涉外酒店"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1103" title="联合国决定继续对刚果(金)实行武器禁运"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1104" title="俄拒绝接受美国进攻性战略武器问题建议"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</channel>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<channel id="12" title="财经">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1201" title="大飞机公司拟定名中国商用飞机有限公司"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1202" title="大部制新部委定编制方案6月底前上报"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</channel>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</channels>
我们的解析处理器的声明如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <xmlcontenthandler.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <xmldocumentparameters.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace Xml;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class TNewsChannel
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TInt id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HBufC16 * title;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class TNewsContent
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TInt id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TInt pid;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HBufC16 * title;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
class CChannelXmlHandler : public MContentHandler ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Constructors and destructor
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
~CChannelXmlHandler();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static CChannelXmlHandler* NewL();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static CChannelXmlHandler* NewLC();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RArray<TNewsChannel>* GetChannels();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RArray<TNewsContent>* GetContents();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TInt GetContent(TInt pid,TInt index);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TInt ContentCount(TInt pid);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CChannelXmlHandler();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void ConstructL();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private: // from MContentHandler
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void OnStartDocumentL( const RDocumentParameters &aDocParam,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TInt aErrorCode );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void OnEndDocumentL( TInt aErrorCode );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void OnStartElementL( const RTagInfo &aElement,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
const RAttributeArray &aAttributes, TInt aErrorCode );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void OnEndElementL( const RTagInfo &aElement, TInt aErrorCode );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void OnContentL( const TDesC8 &aBytes, TInt aErrorCode );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// ... ...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TInt iCurPID;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RArray<TNewsChannel> iChannels;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RArray<TNewsContent> iContents;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
大多数是MContentHandler所声明的方法,这就是SAX事件解析模式的关键了,我们只需要在这些方法中做相应的处理即可。
除此之外,iChannels和iContents是我们定义了用来保存解析结果的成员,它的类型是RArray,关于RArray可以参考我的别一篇笔记:http://blog.csdn.net/sharetop/archive/2008/03/21/2203450.aspx
因为我们的XML比较简单,所以在CPP中只要处理OnStartElementL就可以了:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void CChannelXmlHandler::OnStartElementL( const Xml::RTagInfo &aElement,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
const Xml::RAttributeArray &aAttributes, TInt aErrorCode )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(aElement.LocalName().DesC().Compare(KChannelName)==0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TNewsChannel chn;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(TInt i=0;i<aAttributes.Count();i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(aAttributes[i].Attribute().LocalName().DesC().Compare(KTitleName)==0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
chn.title=CnvUtfConverter::ConvertToUnicodeFromUtf8L(aAttributes[i].Value().DesC());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
else if(aAttributes[i].Attribute().LocalName().DesC().Compare(KIdName)==0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TLex8 lex;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lex.Assign(aAttributes[i].Value().DesC());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lex.Val(chn.id);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
iChannels.Append(chn);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
iCurPID=chn.id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
else if(aElement.LocalName().DesC().Compare(KContentName)==0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TNewsContent cnt;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cnt.pid=iCurPID;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(TInt i=0;i<aAttributes.Count();i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(aAttributes[i].Attribute().LocalName().DesC().Compare(KIdName)==0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TLex8 lex;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lex.Assign(aAttributes[i].Value().DesC());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lex.Val(cnt.id);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
else if(aAttributes[i].Attribute().LocalName().DesC().Compare(KTitleName)==0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cnt.title=CnvUtfConverter::ConvertToUnicodeFromUtf8L(aAttributes[i].Value().DesC());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
iContents.Append(cnt);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
这个回调会在解析器遇到元素头时进入,然后我们就可以根据传入的参数取到当前元素的信息,如元素名称、属性值等,将它们保存在我们定义的数据成员中以备将来使用即可。
在使用这个解析器的地方,比如我们的AppView负责解析XML文件,那它应该包含一个MContentHandler的成员,并且它实现接口MXMLHandlerObserver。
于是,这样启动解析过程:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
iChannelHandler=CChannelXmlHandler::NewL();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
iXmlParser=CXMLActiveParser::NewL(*this,*iChannelHandler);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
iXmlParser->StartL(KChannelXMLFile);
然后在它的OnParseCompleted方法中去iChannelHandler中取出解析结果,展示出来或者随便怎么用了。
书接上回,这篇介绍那个MContentHandler的实现,这是SAX解析方法的核心所在。
先看看我要解析的XML文件如下所示,其实很简单,因为它除了Element和Attribute以外没有其它东西了。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="utf-8" ?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<channels>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<channel id="10" title="时政" >
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1001" title="广东牛奶中毒事件污染源调查结果1周后公布"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1002" title="河南淅川公安局因儿童被拐案设'局耻日'"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1003" title="深圳大学135名师生感染病毒引发腹泻"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</channel>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<channel id="11" title="国际">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1101" title="巴以将于4月7日恢复领导人级和谈"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1102" title="古巴解除长期禁令允许国民入住涉外酒店"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1103" title="联合国决定继续对刚果(金)实行武器禁运"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1104" title="俄拒绝接受美国进攻性战略武器问题建议"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</channel>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<channel id="12" title="财经">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1201" title="大飞机公司拟定名中国商用飞机有限公司"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<content id="1202" title="大部制新部委定编制方案6月底前上报"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</channel>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</channels>
我们的解析处理器的声明如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <xmlcontenthandler.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <xmldocumentparameters.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace Xml;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class TNewsChannel
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TInt id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HBufC16 * title;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class TNewsContent
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TInt id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TInt pid;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HBufC16 * title;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
class CChannelXmlHandler : public MContentHandler ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Constructors and destructor
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
~CChannelXmlHandler();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static CChannelXmlHandler* NewL();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static CChannelXmlHandler* NewLC();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RArray<TNewsChannel>* GetChannels();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RArray<TNewsContent>* GetContents();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TInt GetContent(TInt pid,TInt index);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TInt ContentCount(TInt pid);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CChannelXmlHandler();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void ConstructL();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private: // from MContentHandler
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void OnStartDocumentL( const RDocumentParameters &aDocParam,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TInt aErrorCode );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void OnEndDocumentL( TInt aErrorCode );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void OnStartElementL( const RTagInfo &aElement,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
const RAttributeArray &aAttributes, TInt aErrorCode );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void OnEndElementL( const RTagInfo &aElement, TInt aErrorCode );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void OnContentL( const TDesC8 &aBytes, TInt aErrorCode );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// ... ...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TInt iCurPID;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RArray<TNewsChannel> iChannels;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RArray<TNewsContent> iContents;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
大多数是MContentHandler所声明的方法,这就是SAX事件解析模式的关键了,我们只需要在这些方法中做相应的处理即可。
除此之外,iChannels和iContents是我们定义了用来保存解析结果的成员,它的类型是RArray,关于RArray可以参考我的别一篇笔记:http://blog.csdn.net/sharetop/archive/2008/03/21/2203450.aspx
因为我们的XML比较简单,所以在CPP中只要处理OnStartElementL就可以了:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void CChannelXmlHandler::OnStartElementL( const Xml::RTagInfo &aElement,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
const Xml::RAttributeArray &aAttributes, TInt aErrorCode )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(aElement.LocalName().DesC().Compare(KChannelName)==0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TNewsChannel chn;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(TInt i=0;i<aAttributes.Count();i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(aAttributes[i].Attribute().LocalName().DesC().Compare(KTitleName)==0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
chn.title=CnvUtfConverter::ConvertToUnicodeFromUtf8L(aAttributes[i].Value().DesC());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
else if(aAttributes[i].Attribute().LocalName().DesC().Compare(KIdName)==0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TLex8 lex;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lex.Assign(aAttributes[i].Value().DesC());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lex.Val(chn.id);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
iChannels.Append(chn);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
iCurPID=chn.id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
else if(aElement.LocalName().DesC().Compare(KContentName)==0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TNewsContent cnt;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cnt.pid=iCurPID;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(TInt i=0;i<aAttributes.Count();i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(aAttributes[i].Attribute().LocalName().DesC().Compare(KIdName)==0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TLex8 lex;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lex.Assign(aAttributes[i].Value().DesC());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lex.Val(cnt.id);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
else if(aAttributes[i].Attribute().LocalName().DesC().Compare(KTitleName)==0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cnt.title=CnvUtfConverter::ConvertToUnicodeFromUtf8L(aAttributes[i].Value().DesC());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
iContents.Append(cnt);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
这个回调会在解析器遇到元素头时进入,然后我们就可以根据传入的参数取到当前元素的信息,如元素名称、属性值等,将它们保存在我们定义的数据成员中以备将来使用即可。
在使用这个解析器的地方,比如我们的AppView负责解析XML文件,那它应该包含一个MContentHandler的成员,并且它实现接口MXMLHandlerObserver。
于是,这样启动解析过程:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
iChannelHandler=CChannelXmlHandler::NewL();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
iXmlParser=CXMLActiveParser::NewL(*this,*iChannelHandler);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
iXmlParser->StartL(KChannelXMLFile);
然后在它的OnParseCompleted方法中去iChannelHandler中取出解析结果,展示出来或者随便怎么用了。
相关文章推荐
- Symbian学习笔记(16) - 解析XML文件(下)
- Symbian学习笔记(15) - 解析XML文件(上)
- Symbian学习笔记(15) - 解析XML文件(上)
- Symbian学习笔记——解析XML文件(上)
- Symbian学习笔记——解析XML文件(下)
- Maven学习笔记二eclipse如何使用 Maven、Maven目录pom.xml文件的解析、maven 和maven项目之间的关系
- xml学习笔记②PHP DOM--对xml文件进行解析操作
- 【coder-pig教程学习笔记4】xml文件解析
- Java之JDOM方式解析XML文件(学习笔记)
- 学习笔记之利用ajax请求xml文件,解析其中内容
- IOS学习笔记(16)网络请求,json解析
- 学习笔记-Java中的xml文件读取之DOM4J解析
- glib 学习笔记,解析xml文件
- Android 个人学习笔记之---SAX解析XML文件(有一个坑爹的问题)
- Java之DOM4J方式解析XML文件(学习笔记)
- 慕课学习笔记之DOM,SAX,JDOM,DOM4J解析XML文件
- JSP学习笔记(7)_xml文件解析
- 个人学习笔记之-DOM解析XML文件
- 【学习笔记】JAVA之使用dom4j对xml文件解析
- 个人安卓学习笔记---分别利用SAX、DOM和Pull实现对XML文件的解析并进行单元测试