游戏开发论坛

 找回密码
 立即注册
搜索
查看: 935|回复: 0

XML 初级教程wxh zt

[复制链接]

1367

主题

1993

帖子

2118

积分

金牌会员

Rank: 6Rank: 6

积分
2118
发表于 2005-8-1 21:52:00 | 显示全部楼层 |阅读模式

一、SAX架构
SAX包含多个Handler处理器,其中有诸多函数以处理XML资源的时侯处理事件。
其中ContentHandler是最重要的,它用来对XML文档进行解析,DTDHandler则对DTD文件进行验证。
当前MSXML3包含以下处理器:

IMXAttributes Interface
IMXWriter Interface
ISAXAttributes Interface
ISAXContentHandler Interface
ISAXDeclHandler Interface
ISAXDTDHandler Interface
ISAXEntityResolver Interface
ISAXErrorHandler Interface
ISAXLexicalHandler Interface
ISAXLocator Interface
ISAXXMLFilter Interface
ISAXXMLReader Interface

二、ISAXContentHandler接口
ContentHandler拥有以下函数: characters endDocument startDocument endElement startElement
ignorableWhitespace startPrefixMapping processingInstruction skippedEntity   

今天要讲解的函数是characters,startDocument/endDoucment,startElement/endElement函数,它们的用途如图所示:

三、函数讲解

HRESULT characters(
   [in] const wchar_t * pwchChars,
   [in] int cchChars);
   pwchChars获取字符串信息。
   cchChars获取该字符串长度。
   
HRESULT startDocument();
   该函数表明开始解析文档。
HRESULT startElement(
   [in] const wchar_t * pwchNamespaceUri,
   [in] int cchNamespaceUri,
   [in] const wchar_t * pwchLocalName,
   [in] int cchLocalName
   [in] const wchar_t *
   [in] int cchQName);
   [in] ISAXAttributes * pAttributes);
   pwchNamespaceUri获取命名空间URI。
   cchNamespaceUri获取URI的长度。
   pwchLocalName获取标记。
   cchLocalName获取标记长度。
   pwchQName获取QName。
   cchQName获取QName长度。
   pAttributes获取元素属性信息。          
   
       
  由于元素属性并不固定,需要用如下方法获取。
   pAttributes->getLength(&l);
   for ( int i=0; i<l; i++ ) {
      wchar_t * ln, * vl; int lnl, vll;
      pAttributes->getLocalName(i,&ln,&lnl);
      prt(L" %s=", ln, lnl);
      pAttributes->getValue(i,&vl,&vll);
      prt(L"\"%s\"", vl, vll);
   }

四、 使用ContentHandler:
        HERO_XMLContentHandler* pHero_XML;
        ISAXXMLReaderPtr pReader = NULL;
        HRESULT hr;
        CHR(pReader.CreateInstance(__uuidof(SAXXMLReader)));
        //Only one content handler can be registered at a time.
        pHero_XML=HERO_XMLContentHandler::CreateInstance();        //生成HERO_XMLContentHandler对象。
        CHR(pReader->putContentHandler(pHero_XML));        //注册HERO_XMLContentHandler处理器。
        CHR(pReader->parseURL(L"hero.xml"));//解析路径。
        CHR(pReader->putContentHandler(NULL));

程序运行图。

五、后记

希望本文能给大家帮助,欢迎大家批评指正,具体请看源代码。使用开发包为MSXML3,当然用Apache Xerces原理也是一样的。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-26 16:42

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表