XML簡明教程——文件物件模型——DOM和SAX(一)

來源:互聯網
上載者:User

標籤:sax   基於事件處理   xml   

概述

SAX是基於事件解析XML文檔的代表性技術。SAX通過掃描XML文檔的內容,對元素,屬性和文本資料內容等逐一分析和處理。

1.XML基於時間的解析模式

    基於事件的解析是指XML處理常式線性描述一個XML文檔,當XML處理出鞥許從文檔的開頭至結尾讀取XML文檔的過程中,逐一分析和處理遇到的元素、屬性和字元資料等。

【例子1】奧運金牌榜XML檔案:

<?xml version = "1.0" encoding = "UTF-8"?>

<?xml-stylesheet type="application/xml" href = "priority.xsl"?>

<olympic>

    <olympic_games>

        <year>2008</year>

        <city>Beijing</city>

        <country>China</country>

        <medal_standings>

            <country code = "CHN">

                <name>China</name>

                <rank>1</rank>

                <gold>51<gold>

            </country>

            <country code = "USA">

                <name>United States</name>

                <rank>2</rank>

                <gold>36<gold>

            <country>

            <country code = "RUS">

                <name>Russia</name>

                <rank>3</rank>

                <gold>23</gold>

            <country>

        </medal_standings>

    </olympic_games>

<olympic>

上面例子是一個普通的xml文檔,包含了元素、元素的屬性、子項目和元素常值內容、處理指示等主要組成部分。當暫時不考慮屬性和指令,XML處理常式將依次按照元素的起始和結束標籤等判斷元素的編輯範圍以及元素內容,並且能夠判斷元素的層次關係,最終嚮應用程式報告當前這個你在處理的內容。一個簡單的基於事件處理的XML處理常式解析例子,能夠得到如下解析結果:

Start Document


Start Element: olympic

Characters:


Start Element:year

Characters:2008

End Element:year

Characters:


Start Element:city

Characters:Beijing

End Element:city

Characters:


Start Element:country

Characters:China

End Element:country

Characters:


Start Element: medal_standings

Characters: 


Start Element: country

Characters: 


Start Element: name

Characters: China

End Element: name

Characters: 


Start Element: rank

Characters: 1

End Element:rank 

Characters: 


Start Element: gold

Characters: 51

End Element: gold

Characters: 


End Element: country

Characters:


Start Element: Country

Characters: 


Start Element: name

Characters: United States

End Element: name

Characters: 


Start Element: rank

Characters: 2

End Element:rank 

Characters: 


Start Element: gold

Characters: 36

End Element: gold

Characters: 


End Element: country

Characters: 


Start Element: country

Characters: 


Start Element: name

Characters: Russia

End Element: name

Characters: 


Start Element: rank

Characters: 3

End Element:rank 

Characters: 


Start Element: gold

Characters: 3

End Element:gold 

Characters: 


End Element: country

Characters: 


Start Element: medal_standings

Characters: 


Start Element: olympic_games

Characters: 


Start Element: olympic


End Document

    以上是對例子的一個簡單的解析過程,整個過程以XML文檔的開始和結尾作為起點和終點。處理過程中的事件包括了文檔開始、文檔結束、元素開始、元素結束、元素的常值內容等。由於XML處理常式是按照從XML文檔中的讀取的資料單元進行解析,而不是先進行對XML文檔做整體分析,因此及時XML文檔不符合形式良好的要求,XML處理常式頁並不能事先發現這些錯誤,知識當正在處理的資料片段與先前已經處理完成的部分發生衝突時,比如結束標籤缺失或標籤嵌套錯誤,才會停止解析並報告錯誤。

    處理速度和效率是基於事件的解析方式的一大優勢,尤其是對於體積龐大的大興XML文檔,由於不需要把大量文檔中的資料載入到內容,從資源利用的角度看,效率也是比較高的。從另外一個角度看,基於事件的XML解析過程由於需要暫存每一次處理步驟的狀態,由於分析XML文檔是否滿足形式良好的要求和其他有效性約束條件,應用程式必須能夠處理隨時可能發生的錯誤,因此對於基於事件的處理方式來說,處理過程相對比較複雜。

(詳細內容見《XML簡明教程》——張欣毅,著)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.