UIQ3开发问题集——解析带中文的XML文件

http://tech.ddvip.com   2008年09月08日    社区交流

本文详细介绍UIQ3开发问题集——解析带中文的XML文件

  1.将XML文件使用记事本UTF-8编码

  2.include utf.h xmlcontenthandler.h xmlparser.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 );
}

来源:索爱开发社区    作者:佚名    责编:豆豆技术应用

正在加载评论...