首 页 | 新 闻 | Symbian | Android| Windows Mobile | J2ME | 下载中心 | 游戏策划招聘与求职 | 购书指南 | 视频教程
您现在的位置: 开发视界 >> Symbian >> Symbian入门 >> 文章正文
UIQ3开发问题集——解析带中文的XML文件
作者:佚名    文章来源:索爱开发社区    更新时间:2008-2-29 13:06:36
1.将XML文件使用记事本UTF-8编码
2.include utf.h xml\contenthandler.h xml\parser.h
3.紧接着include添加using namespace Xml;
4.添加xmlframework.lib到mmp的library列表
5.实现一个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 );
6.执行解析过程
...
_LIT8( KXmlMimeType, "text/xml" );
CParser* iParser = CParser::NewL( KXmlMimeType, *this );
iParser->ParseBeginL();
const int KMaxBufLen = 128;
TBuf8<KMaxBufLen> buf;
TBool isFinal = ETrue;
do
{
User::LeaveIfError( file.Read( buf, KMaxBufLen ));
isFinal = ( buf.Length() != KMaxBufLen );
iParser->ParseL( buf );

while( !isFinal );
iParser->ParseEndL();
...
7.在OnStartElementL接口中接收解析的XML字段,注意,在此要调用ConvertToUnicodeFromUtf8进行转换,用法参见《UIQ3开发问题集 - 动态装载带中文的文本文件》一文。
void Cxxx::OnStartElementL( const RTagInfo &aElement, const RAttributeArray &aAttributes, TInt aErrorCode )
...
TPtrC8 elemName( aElement.LocalName().DesC());
HBufC* tmpbuf = NULL;

if ( elemName.Length() == 0 ) return;
const TInt attrCount = aAttributes.Count();
for ( TInt i = 0; i < attrCount; i++)
{
const RAttribute& attribute = aAttributesDes());
CnvUtfConverter::ConvertToUnicodeFromUtf8(tmpbufptr,attrValue);
...
SAFE_DELETE( tmpbuf );
}
...
相关文章:
UIQ3开发问题集——Symbian各平台SDK的版本标识宏
UIQ3开发问题集——动态装载带中文的文本文件