symbian下面xml的解析器
通常我们开发大型应用程序的时候,为了实现程序的可配置,可扩展,以及向下兼容的特点,或多或少的使用了xml的思想,甚至很多朋友都直接使用了xml做为数据传输的格式。
xml标记对于pc上来说,它的解析器种类繁多,这里就不一一列举了,
但是对于手机平台来说,只有少数的几种可供开发者使用的,
Expat--Mozilla浏览器(for linux)的http 1.1和xml解析器,
它的大名可能用过mozilla的人都不需要解释什么了
这原本只是为了mozilla专门开发的解析器引擎,后来被别人改写成为一个
通用的开发包,可以用于用户自己开发的应用程序中去。
它的作者之一,为了考察expat在arm低速处理器上的解析效率,
专门移植了一个Expat的Symbian版本,用于测试。。。
这也就是Syexpat开源项目的由来。(附整个项目的源代码S60 V1.2 SDK下编译通过)
xml本身是由许许多多tag组成的,看上去非常类似html的语法格式,
只不过xml的格式,也就是那个tag是可以用户自己定义的,更加可以更加准确的代表tag里面数据
对于xml解析器,就是用来解析用户自己定义的xml格式的文件的。
使用这个解析器的大体思路就是自己编写每个tag的处理函数,然后,
在解析器引擎上注册这些处理函数,然后用普通的方式读取xml数据文件到一个buf中,
把这个buf交给解析器去处理,解析器在解析buf内容的时候,如果遇到了tag,
它就会自动调用刚刚注册的处理函数。
这样我们就可以把精力专著于如何开发tag的标记和处理tag数据上面了。
有了它的帮助,我相信即使开发一个浏览器,也不是非常复杂的事情了。
下面是symbian s60下的一个调用解析器的例子程序:
希望对大家有帮助
/*****************************************************************
* Elements.cpp
*
* Copyright 1999, Clark Cooper, 2004 ToddSoftware
* All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the license contained in the
* COPYING file that comes with the expat distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Read an XML document that is stored in a string and display the results
*
* Almost all the guts of the system revolve around the doExampleL()
* function which does everything important.
* The rest is just plumbing
*/
#include <e32base.h>
#include "SyExpat.h"
#include <e32test.h>
#include "xmlhelper.h"
// String constants
_LIT(KTxtEPOC32EX,"EXAMPLES");
_LIT(KTxtExampleCode,"Expat Elements");
_LIT(KTxtParsedOk, "XML Successfully parser");
_LIT(KTxtParsedFailedOn, "XML Failed on Line %d Column %d");
_LIT(KTxtFailed, "Failed parsing");
_LIT(KTxtOk, "Ok");
_LIT(KTxtPressAnyKey," [press any key]");
LOCAL_D RTest test(_L("XML String"));
LOCAL_D CTrapCleanup* theCleanup;
LOCAL_C void doExampleL()
{
// 1. Create the XML Parser and set the default document handler
CDocumentHandler xmlOutput(test);
CDeclarationHandler xmlOutput2(test);
#define _CONTEXTUAL_PARSER
#ifdef _CONTEXTUAL_PARSER
CSyContextualParser* reader = CSyContextualParser::NewLC(TPtrC());
reader->PushDocHandler(&xmlOutput);
reader->PushDeclHandler(&xmlOutput2);
#else
CSyParser* reader = CSyParser::NewLC(TPtrC());
reader->SetHandler(&xmlOutput);
reader->SetHandler(&xmlOutput2);
#endif
_LIT8(KDocument, "<?xml version=\"1.0\" standalone=\"yes\"?>"
"<test>"
" <elem1>"
" <elem11>"
"cdata11"
" </elem11>"
" <elem12>"
"cdata12"
" </elem12>"
" </elem1>"
"<!-- This is a comment -->"
" <elem2 attr=\"test\" attr2=\"test2\" attr3=\"test3\"/>"
" <elem3 attr=\"test3\"/>"
" <elem4>"
"text"
" </elem4>"
"</test>");
TPtrC8 doc(KDocument);
TSyExpatStatus status = reader->Parse(doc, ETrue);
if (status == EStatusOk)
{
test.Printf(KTxtParsedOk);
}
else
{
test.Printf(KTxtParsedFailedOn, reader->GetCurrentLineNumber(), reader->GetCurrentColumnNumber());
}
#ifdef _CONTEXTUAL_PARSER
reader->PopDocHandler();
reader->PopDeclHandler();
#endif
// finally clean up the parser and return
CleanupStack::PopAndDestroy();
}
LOCAL_C void callExampleL() // initialize and call example code under cleanup stack
{
TRAPD(error,doExampleL()); // perform example function
if (error)
test.Printf(KTxtFailed, error);
else
test.Printf(KTxtOk);
test.Printf(KTxtPressAnyKey);
test.Getch(); // get and ignore character
}
GLDEF_C TInt E32Main() // main function called by E32
{
__UHEAP_MARK;
test.Start(_L("ParseXMLString"));
test.Title();
theCleanup=CTrapCleanup::New();
TRAPD(ret,callExampleL());
delete theCleanup;
test.End();
test.Close();
__UHEAP_MARKEND;
return(KErrNone);
}
页:
[1]