<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      XML簡易圖文詳細教程之二

      [摘要]文檔格式的排錯:我媽媽_的清單中有數十條菜譜,甚至數百條。如果產生一個致命錯誤,排錯將非常困難 - 你將一行一行地尋找丟失的標記符。如果使用幾層嵌套,發現錯誤將很困難。但是可以找到很好的幫助。分析器...

      文檔格式的排錯:我媽媽_的清單中有數十條菜譜,甚至數百條。如果產生一個致命錯誤,排錯將非常困難 - 你將一行一行地尋找丟失的標記符。如果使用幾層嵌套,發現錯誤將很困難。

      但是可以找到很好的幫助。分析器 - XML代碼和報告格式錯誤的應用程序可以在網上免費得到。其中最好的是Lark,它的作者是由Tim Bray - XML規范的技術編輯和極力鼓吹者,地球上最聰明的人之一。

      我用Lark分析下面的代碼。注意"chocolate chips"和它的關閉標記符出現在</ingredients> 標記符中的位置有錯誤:

      <?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>

      下面是分析器返回的結果:

      Error Report

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

      ... assumed </item>

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

      有了這種信息,找到錯誤將不會成為問題。那么XML文件的有效性是指什么呢?

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

      <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>
      這份菜譜中沒有包含ingredient,而且因為它組織良好,所以Lark分析器也不會發現問題。管理過哪怕是最和善的數據庫的人都知道我們人類常犯的錯誤:如果有機會,我們會丟掉關鍵信息并加入無用的廢話。這就是為什么XML的發明者引入DTD - 文檔類型定義(Document Type Definition)。DTD提供了一種保證XML或多或少是你所想的方法。

      讓我們看看用在菜譜上的一個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)>
      ]>
      這些代碼起初看起來不夠友好,但當把它分解時卻能看出其中的意義。讓我們詳細解釋之:

      <!DOCTYPE list [

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

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

      這行定義了<recipe>標記符。圓括號是說其中的四種標記符必須按照順序出現在<recipe>標記符中。

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

      這行需要詳細的解釋。我定義了以下的結構:

      <meal>Here the meal name is mandatory
      <course>One course name may appear, but it is not
      mandatory</course>
      </meal>
      我這樣做是因為,按照我的想法,午餐不一定特定某道菜,但是晚餐可能要指出開胃食品、主菜和餐后甜點。通過指定#PCDATA - 表示經過分析的字符數據(即非二進制數據)來實現這個功能。這里,#PCDATA是文本 - 例如,“dinner”。

      "course"后面的問號表示0或1對<course>標記符將出現在<meal>標記符內。

      現在讓我們看看下一行:

      <!ELEMENT ingredients (item+)>

      這里的加號表示至少有一對<item>標記符應出現在<ingredients>標記符內。

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

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

      我把sub_item*作為一項安全措施。除了要求每個item的文本之外,我希望計算每個item的內容的數量。星號是說在<item>標記符中可以有子條目的數目。我不需要Chocolate Chip Bars菜譜的任何子條目,但是當它的組成成分很復雜時就用得著。

      現在讓我們把這些放在一起看看我們能得到什么。

      DTD的完整例子
      下面是一個完整的例子。我把另一個菜譜加入文件內,并為DTD做了注釋?梢宰⒁獾轿以诘诙䝼菜譜中用到子條目。

      <?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做出的限制。換句話說,我們要保證文檔的有效性。

      為了達到這個目的,我們需要另一個工具:有效性分析器。微軟的MSXML,一個基于Java的程序,使用容易又工作得很好。上面的文檔經過這個程序的檢查后沒有發現錯誤。但是如果我檢查一個ingredient標記符中沒有包含條目的菜譜,將會返回以下信息:

      ingredients is not complete. Expected elements [item].





      日韩精品一区二区三区高清