• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            woomsg

            在路上

            gloox代碼分析2 - xml parser模塊

            gloox自己實(shí)現(xiàn)了xml的解析模塊,沒有用到第三方的庫(tinyXML,expat )
            主要涉及的文件:
            tag.h (tag.cpp)
            taghandler.h
            parser.h (parser.cpp)

            1. Tag一個(gè)Tag就是一個(gè)XML元素
            例如:
            a.
            <book kind='computer'>
              <store id='23'/>
              <author>
                qiang
              </author>
            </book>
            b. <book id='32'/>
            c. <book>name1</book>

            首先介紹一個(gè)概念: escape-string,何為escape-string?
            在escape-string中:
             '&'轉(zhuǎn)換成&amp;, '<'轉(zhuǎn)換成&lt;, '>'轉(zhuǎn)換成&gt;.
            編碼表如下:
            //////////////////////////////////////////////////////////////////////////
            // 編碼表 (中間的空格去掉,這里只是為了方便顯示):
            // -------------------------------------------------------
            // | 字符     | 十進(jìn)制 | 十六進(jìn)制 | THML字符集 | Unicode |
            // -------------------------------------------------------
            // | " 雙引號(hào) | & # 34;  | & # x22;   | "          | \u0022  |
            // -------------------------------------------------------
            // | ' 單引號(hào) | & # 39;  | & # x27;   | & apos;     | \u0027  |
            // -------------------------------------------------------
            // | & 與     | & # 38;  | & # x26;   | & amp;      | \u0026  |
            // -------------------------------------------------------
            // | < 小于號(hào) | & # 60;  | & # x3C;   | & lt;       | \u003c  |
            // -------------------------------------------------------
            // | > 大于好 | & # 62;  | & # x3E;   | & gt;       | \u003e  |
            // -------------------------------------------------------
            gloox - APIs
            Tag::escape()    功能: string -> escape-string
            Tag::relax()  功能: escape-string -> string

            主要成員變量:
            attributes - 所有屬性的list
            name - 節(jié)點(diǎn)名字
            cdata - 節(jié)點(diǎn)數(shù)據(jù),例如<name>cdata</name>中的cdata
            children - 所有的子節(jié)點(diǎn)
            parent - 父節(jié)點(diǎn)指針,如果沒有則為空
            bool incoming - 表示構(gòu)造xml node的時(shí)候傳入的字符串是否是escape-string,如果是,需要在構(gòu)造的時(shí)候調(diào)用relex把escape-string轉(zhuǎn)換成string.

            主要方法:
            也就是一些針對(duì)name\children\attributes\cdata進(jìn)行增加\刪除\修改的方法
            xml()方法返回該節(jié)點(diǎn)的一個(gè)完整的xml數(shù)據(jù)流
            findTag和findTagList提供對(duì)XPath的支持.

            例如:
            屏幕將輸出:
            <book kind='computer'><store id='23'/><author>qiang</author></book>
             1#include <iostream>
             2#include "tag.h"
             3
             4#pragma comment( lib, "gloox.lib" )
             5using namespace gloox;
             6
             7// <book kind='computer'>
             8//   <store id='23'/>
             9//   <author>
            10//     qiang
            11//   </author>
            12// </book>
            13//
            14
            15
            16int main( int argc, char* argv[] ) {
            17  Tag* tag_book = new Tag( "book");
            18  tag_book->addAttribute( "kind""computer" );
            19  
            20  Tag* tag_store = new Tag( "store" );
            21  tag_store->addAttribute( "id""32" );
            22
            23  Tag* tag_author = new Tag( "author""qiang" );
            24
            25  tag_book->addChild( tag_store );
            26  tag_book->addChild( tag_author );
            27
            28  std::cout<<tag_book->xml()<<std::endl;
            29  return 0;
            30}

            2. TagHandler是一個(gè)接收parser解析完成的tag的接口,繼承該類,則可以接收parser解析的tag對(duì)象事件.
            只有一個(gè)接口
            virtual void handleTag( Tag *tag ) = 0 - 接收解析完的tag

            3. Parser一個(gè)XML解析器
            提供的接口非常簡(jiǎn)潔,只需要一個(gè)TagHandler來構(gòu)造,該handler接收并處理解析的tag,另外只有一個(gè)feed接口來填充數(shù)據(jù).
            要注意的是feed接口填充的數(shù)據(jù)必須是一個(gè)格式正確的xml,否則無法解析,也就是說parser不會(huì)判斷xml的格式。

            例如:
            下面的例子中對(duì)feed來說分開填充和一次性填充數(shù)據(jù)的效果是一樣的,也就是scenario1和scenario2的效果是一樣的,這也剛好和上層應(yīng)用中TCP 流處理的方式統(tǒng)一,對(duì)于接收到服務(wù)器端的XML流,無論是否完整,只需要直接feed就可以了。handlerTag方法將收到兩個(gè)
            xml tag解析完成的事件,分別來自scenario1和scenario2,屏幕將輸出:
            <book kind='computer'><store id='23'/><author>qiang</author></book> 
            <book kind='computer'><store id='23'/><author>qiang</author></book>
             1#include <iostream>
             2#include "tag.h"
             3#include "parser.h"
             4
             5#pragma comment( lib, "gloox.lib" )
             6using namespace gloox;
             7
             8// <book kind='computer'>
             9//   <store id='23'/>
            10//   <author>
            11//     qiang
            12//   </author>
            13// </book>
            14//
            15//
            16
            17class TagHandlerImpl : public TagHandler {
            18public:
            19    ~TagHandlerImpl() {}
            20
            21    void run() {
            22      Parser* parser = new Parser(this);
            23      // scenario1
            24      std::string data = "<book kind='computer'><store id='23'/><author>qiang</author></book>";
            25      parser->feed( data );
            26
            27      // scenario2
            28      std::string data1 = "<book kind='computer";
            29      std::string data2 = "'><store id='23'/><auth";
            30      std::string data3 = "or>qiang</author></book>";
            31      parser->feed( data1 );
            32      parser->feed( data2 );
            33      parser->feed( data3 );
            34    }

            35
            36    void handleTag( Tag *tag ) {
            37      std::cout<<tag->xml()<<std::endl;
            38    }

            39}
            ;
            40
            41int main( int argc, char* argv[] ) {
            42  TagHandlerImpl* taghandlerImpl = new TagHandlerImpl();
            43  taghandlerImpl->run();
            44
            45  return 0;
            46}


            posted on 2008-10-18 14:51 ysong.lee 閱讀(2580) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久精品国产清自在天天线| 精品久久久久久中文字幕| yellow中文字幕久久网| 久久久久免费视频| 综合久久一区二区三区| 久久ZYZ资源站无码中文动漫| 日韩精品久久久久久| 久久99精品国产麻豆宅宅| 久久精品国产99国产精偷| 久久婷婷五月综合成人D啪| 久久se这里只有精品| 久久电影网一区| 久久久久久久久久久精品尤物| 久久AV高清无码| 久久精品毛片免费观看| 久久久久亚洲精品天堂久久久久久 | 亚洲国产精品久久电影欧美| 久久精品国产清自在天天线| 亚洲国产精品高清久久久| 久久精品国产第一区二区| 国产麻豆精品久久一二三| 超级碰碰碰碰97久久久久| 思思久久99热只有频精品66| 久久亚洲精品无码观看不卡| 91精品国产91久久久久福利| 久久婷婷五月综合色奶水99啪| 久久久午夜精品福利内容| 久久99热这里只有精品国产| 九九99精品久久久久久| 国产精品久久久久久吹潮| 伊人久久大香线焦AV综合影院| 久久无码高潮喷水| 久久久久无码精品| 久久久久人妻精品一区三寸蜜桃| 99久久成人18免费网站| 久久久久亚洲爆乳少妇无 | 青青草原精品99久久精品66| 囯产精品久久久久久久久蜜桃| 久久国产成人午夜aⅴ影院| 精品久久久无码中文字幕天天| 国产99久久久久久免费看|