淺談XML的簡(jiǎn)易教程_Xml教程

      編輯Tag賺U幣
      教程Tag:暫無(wú)Tag,歡迎添加,賺取U幣!

      推薦:解析XML輕松學(xué)習(xí)手冊(cè)(3)XML的術(shù)語(yǔ)
      第三章 XML的術(shù)語(yǔ)提綱: 導(dǎo)言 一.XML文檔的有關(guān)術(shù)語(yǔ) 二.DTD的有關(guān)術(shù)語(yǔ) 導(dǎo)言 初學(xué)XML最令人頭疼的就是有一大堆新的術(shù)語(yǔ)概念要理解。由于XML本身也是一個(gè)嶄新的技術(shù),正在不斷發(fā)展和變化,各組織

      文檔格式的排錯(cuò)
      我媽媽_的清單中有數(shù)十條菜譜,甚至數(shù)百條。如果產(chǎn)生一個(gè)致
      命錯(cuò)誤,排錯(cuò)將非常困難 - 你將一行一行地尋找丟失的標(biāo)記
      符。如果使用幾層嵌套,發(fā)現(xiàn)錯(cuò)誤將很困難。

      但是可以找到很好的幫助。分析器 - XML代碼和報(bào)告格式錯(cuò)誤
      的應(yīng)用程序可以在網(wǎng)上免費(fèi)得到。其中最好的是Lark,它的作
      者是由Tim Bray - XML規(guī)范的技術(shù)編輯和極力鼓吹者,地球上最
      聰明的人之一。

      我用Lark分析下面的代碼。注意"chocolate chips"和它的關(guān)閉
      標(biāo)記符出現(xiàn)在</ingredients> 標(biāo)記符中的位置有錯(cuò)誤:

      <?xml version="1.0"?>

      <list>

      <recipe>

      <author>Carol Schmidt</author>

      <recipe_name>Chocolate Chip Bars</recipe_name>

      <meal>Dinner

      <course>Dessert</course>

      </meal>

      <ingredients>

      <item>2/3 C butter</item>

      <item>2 C brown sugar</item>

      <item>1 tsp vanilla</item>

      <item>1 3/4 C unsifted all-purpose flour</item>

      <item>1 1/2 tsp baking powder</item>

      <item>1/2 tsp salt</item>

      <item>3 eggs</item>

      <item>1/2 C chopped nuts</item>

      <item>

      </ingredients>2 cups (12-oz pkg.) semi-sweet choc.

      chips</item>

      <directions>

      Preheat overn to 350 degrees. Melt butter;

      combine with brown sugar and vanilla in large mixing bowl.

      Set aside to cool. Combine flour, baking powder, and salt; set aside.

      Add eggs to cooled sugar mixture; beat well. Stir in reserved dry

      ingredients, nuts, and chips.

      Spread in greased 13-by-9-inch pan. Bake for 25 to 30 minutes

      until golden brown; cool. Cut into squares.

      </directions>

      </recipe>

      </list>

      下面是分析器返回的結(jié)果:

      Error Report

      Line 17, column 22: Encountered </ingredients> expected </item>

      ... assumed </item>

      Line 18, column 36: Encountered </item> with no start-tag.

      有了這種信息,找到錯(cuò)誤將不會(huì)成為問(wèn)題。那么XML文件的有效性
      是指什么呢?

      實(shí)現(xiàn)有效性
      最終我們將在組織良好的XML文檔中加入信息。實(shí)際上,我們
      有很多事要做 - 仍然有危機(jī)潛伏 - 雖然XML文件組織良好,
      但還可能丟失關(guān)鍵信息。看看下面的例子:

      <recipe>
      <author>Carol Schmidt</author>
      <recipe_name>Chocolate Chip Bars</recipe_name>
      <meal>Dinner <course>Dessert</course> </meal>
      <ingredients> </ingredients>
      <directions>Melt butter; combine with, etc. ... </directions>
      </recipe>
      這份菜譜中沒(méi)有包含ingredient,而且因?yàn)樗M織良好,所以
      Lark分析器也不會(huì)發(fā)現(xiàn)問(wèn)題。管理過(guò)哪怕是最和善的數(shù)據(jù)庫(kù)的
      人都知道我們?nèi)祟?lèi)常犯的錯(cuò)誤:如果有機(jī)會(huì),我們會(huì)丟掉關(guān)鍵
      信息并加入無(wú)用的廢話。這就是為什么XML的發(fā)明者引入DTD -
      文檔類(lèi)型定義(Document Type Definition)。DTD提供了一種保
      證XML或多或少是你所想的方法。

      讓我們看看用在菜譜上的一個(gè)DTD。

      <!DOCTYPE list [
      <!ELEMENT recipe (recipe_name, author, meal, ingredients, directions)>
      <!ELEMENT ingredients (item )>
      <!ELEMENT meal (#PCDATA, course?)>
      <!ELEMENT item (#PCDATA, sub_item*)>
      <!ELEMENT recipe_name (#PCDATA)>
      <!ELEMENT author (#PCDATA)>
      <!ELEMENT course (#PCDATA)>
      <!ELEMENT item (#PCDATA)>
      <!ELEMENT subitem (#PCDATA)>
      <!ELEMENT directions (#PCDATA)>
      ]>
      這些代碼起初看起來(lái)不夠友好,但當(dāng)把它分解時(shí)卻能看出其中
      的意義。讓我們?cè)敿?xì)解釋之:

      <!DOCTYPE list [

      這行是說(shuō),包含在方括號(hào)中的是具有根元素<list>的某個(gè)文檔的
      DTD。如我們以前提到的,根元素包含所有其它元素。

      <!ELEMENT recipe (recipe_name, meal, ingredients, directions)>

      這行定義了<recipe>標(biāo)記符。圓括號(hào)是說(shuō)其中的四種標(biāo)記符必
      須按照順序出現(xiàn)在<recipe>標(biāo)記符中。

      <!ELEMENT meal (#PCDATA, course?)>

      這行需要詳細(xì)的解釋。我定義了以下的結(jié)構(gòu):

      <meal>Here the meal name is mandatory
      <course>One course name may appear, but it is not
      mandatory</course>
      </meal>
      我這樣做是因?yàn)椋凑瘴业南敕ǎ绮筒灰欢ㄌ囟车啦耍?br />是晚餐可能要指出開(kāi)胃食品、主菜和餐后甜點(diǎn)。通過(guò)指定
      #PCDATA - 表示經(jīng)過(guò)分析的字符數(shù)據(jù)(即非二進(jìn)制數(shù)據(jù))來(lái)
      實(shí)現(xiàn)這個(gè)功能。這里,#PCDATA是文本 - 例如,“dinner”。

      "course"后面的問(wèn)號(hào)表示0或1對(duì)<course>標(biāo)記符將出現(xiàn)在<meal>
      標(biāo)記符內(nèi)。

      現(xiàn)在讓我們看看下一行:

      <!ELEMENT ingredients (item )>

      這里的加號(hào)表示至少有一對(duì)<item>標(biāo)記符應(yīng)出現(xiàn)在<ingredients>
      標(biāo)記符內(nèi)。

      我們感興趣的最后一行是:

      <!ELEMENT item (#PCDATA, sub_item*)>

      我把sub_item*作為一項(xiàng)安全措施。除了要求每個(gè)item的文本之
      外,我希望計(jì)算每個(gè)item的內(nèi)容的數(shù)量。星號(hào)是說(shuō)在<item>標(biāo)記
      符中可以有子條目的數(shù)目。我不需要Chocolate Chip Bars菜譜的
      任何子條目,但是當(dāng)它的組成成分很復(fù)雜時(shí)就用得著。

      現(xiàn)在讓我們把這些放在一起看看我們能得到什么。

      DTD的完整例子
      下面是一個(gè)完整的例子。我把另一個(gè)菜譜加入文件內(nèi),并為
      DTD做了注釋。可以注意到我在第二個(gè)菜譜中用到子條目。

      <?xml version="1.0"?>
      <!--This starts the DTD. The first four lines address document structure-->
      <!DOCTYPE list ][
      <!ELEMENT recipe (recipe_name, author, meal, ingredients,directions)>
      <!ELEMENT ingredients (item )>
      <!ELEMENT meal (#PCDATA, course?)>
      <!ELEMENT item (#PCDATA, sub_item*)>
      <!--These are the remaining elements of the recipe tag -->
      <!ELEMENT recipe_name (#PCDATA)>
      <!ELEMENT author (#PCDATA)>
      <!ELEMENT directions (#PCDATA)>
      <!--The remaining element of the meal tag -->
      <!ELEMENT course (#PCDATA)>
      <!--The remaining element of the item tag -->
      <!ELEMENT sub_item (#PCDATA)>
      ]>
       

      <?xml version="1.0"?>
      <list>
      <recipe>
      <author>Carol Schmidt</author>
      <recipe_name>Chocolate Chip Bars</recipe_name>
      <meal>Dinner
      <course>Dessert</course>
      </meal>
      <ingredients>
      <item>2/3 C butter</item>
      <item>2 C brown sugar</item>
      <item>1 tsp vanilla</item>
      <item>1 3/4 C unsifted all-purpose flour</item>
      <item>1 1/2 tsp baking powder</item>
      <item>1/2 tsp salt</item>
      <item>3 eggs</item>
      <item>1/2 C chopped nuts</item>
      <item>2 cups (12-oz pkg.) semi-sweetchoc. chips</item>
      </ingredients>
      <directions>
      Preheat oven to 350 degrees. Melt butter;
      combinewith brown sugar and vanilla in large mixing bowl.
      Set aside to cool. Combine flour, baking powder, andsalt;
      set aside.Add eggs to cooled sugar mixture; beat well.
      Stir in reserved dry ingredients, nuts, and chips.
      Spread in greased 13-by-9-inch pan.
      Bake for 25 to 30minutes until golden brown; cool.
      Cut into squares.
      </directions>
      </recipe>
      <recipe>
      <recipe_name>Pasta with tomato Sauce</recipe_name>
      <meal>Dinner
      <course>Entree</course>
      </meal>
      <ingredients>
      <item>1 lb spaghetti</item>
      <item>1 16-oz can diced tomatoes</item>
      <item>4 cloves garlic</item>
      <item>1 diced onion</item>
      <item>Italian seasoning
      <sub_item>oregano</sub_item>
      <sub_item>basil</sub_item>
      <sub_item>crushed red pepper</sub_item>
      </item>
      </ingredients>
      <directions>
      Boil pasta. Sauté garlic and onion.
      Add tomatoes.Serve hot.
      </directions>
      </recipe>
      </list>
      既然有DTD,文檔將被檢查看是否符合DTD做出的限制。換句話
      說(shuō),我們要保證文檔的有效性。

      為了達(dá)到這個(gè)目的,我們需要另一個(gè)工具:有效性分析器。微軟
      的MSXML,一個(gè)基于Java的程序,使用容易又工作得很好。上面的
      文檔經(jīng)過(guò)這個(gè)程序的檢查后沒(méi)有發(fā)現(xiàn)錯(cuò)誤。但是如果我檢查一個(gè)
      ingredient標(biāo)記符中沒(méi)有包含條目的菜譜,將會(huì)返回以下信息:

      ingredients is not complete. Expected elements [item].

      分享:解讀WEB頁(yè)面工具語(yǔ)言XML(五)好處
      五、XML帶來(lái)的好處 (1)更有意義的搜索 數(shù)據(jù)可被XML唯一的標(biāo)識(shí)。沒(méi)有XML,搜索軟件必須了解每個(gè)數(shù)據(jù)庫(kù)是如何構(gòu)建的。這實(shí)際上是不可能的,因?yàn)槊總(gè)數(shù)據(jù)庫(kù)描述數(shù)據(jù)都是不同的。有了XML,書(shū)就可

      來(lái)源:模板無(wú)憂//所屬分類(lèi):Xml教程/更新時(shí)間:2009-05-16
      相關(guān)Xml教程