XML 的用途

XML 被用于 Web 開發的方方面面。

XML 通常用于分離數據與呈現。

XML 將數據與呈現分離

XML 不攜帶任何有關如何顯示數據的信息。

相同的 XML 數據可以用于許多不同的呈現場景。

因此,通過 XML,數據和呈現之間完全分離。

XML 通常是對 HTML 的補充

在許多 HTML 應用程序中,XML 用于存儲或傳輸數據,而 HTML 用于格式化和顯示這些數據。

XML 將數據與 HTML 分離

在使用 HTML 顯示數據時,您不必在數據更改時編輯 HTML 文件。

通過 XML,數據可以存儲在單獨的 XML 文件中。

只需幾行 JavaScript 代碼,您就可以讀取 XML 文件,然后更新 HTML 中的數據內容。

Books.xml

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="美食">
  <title lang="zh">雅舍談吃</title>
  <author>梁實秋</author>
  <press>江蘇文藝出版社</press>
  <year>2013</year>
  <price>35</price>
  <ISBN>9787539962771</ISBN>
</book>
<book category="兒童">
  <title lang="zh">了不起的狐貍爸爸</title>
  <author>羅爾德·達爾</author>
  <translator>代維</translator>
  <press>明天出版社</press>
  <year>2009</year>
  <price>10</price>
  <ISBN>9787533259563</ISBN>
</book>
<book category="文學">
  <title lang="zh">將熟悉變為陌生</title>
  <author>齊格蒙·鮑曼</author>
  <author>彼得·哈夫納</author>
  <translator>王立秋</translator>
  <press>南京大學出版社</press>
  <year>2023</year>
  <price>68</price>
  <ISBN>9787305269387</ISBN>
</book>
<book category="科學">
  <title lang="zh">你想飛嗎,像鳥一樣?</title>
  <author>理查德·道金斯</author>
  <author>亞娜·倫佐娃</author>
  <translator>高天羽</translator>
  <press>湖南科學技術出版社</press>
  <year>2023</year>
  <price>88</price>
  <ISBN>9787571019075</ISBN>
</book>
<book category="政治" cover="平裝">
  <title lang="zh">論美國的民主</title>
  <author>托克維爾</author>
  <translator>董果良</translator>
  <press>商務印書館</press>
  <year>1989</year>
  <price>60</price>
  <ISBN>9787100124553</ISBN>
</book>
</bookstore>

您將在本教程的 DOM 部分中學習關于使用 XML 和 JavaScript 的更多內容。

交易數據

眾多行業中存在數以千計的 XML 格式,描述日復一日的數據交換:

  • 股票和股份
  • 金融交易
  • 醫療數據
  • 數學數據
  • 科學測量
  • 新聞資訊
  • 天氣服務

實例:XML 新聞

XMLNews 是交換新聞和其他信息的規范。

使用標準的好處是:可以讓新聞工作者和新聞消費者更輕松地跨不同硬件、軟件和編程語言,制作、接收并存儲任意類型的新聞信息。

XMLNews 文檔實例:

<?xml version="1.0" encoding="UTF-8"?>
<nitf>
  <head>
    <title>東航C919首次執飛京滬航線</title>
  </head>
  <body>
    <headline>
      <hl1>東航C919首次執飛上海虹橋往返北京大興航線</hl1>
    </headline>
    <byline>
      <bytag>中央廣播電視總臺</bytag>
    </byline>
    <dateline>
      <location>上海虹橋國際機場</location>
      <date>2024年01月09日</date>
    </dateline>
  </body>
</nitf>

實例:XML 天氣服務

NOAA(國家海洋和大氣管理局)提供的 XML 國家氣象服務:

<?xml version="1.0" encoding="UTF-8"?>
<current_observation>
<credit>NOAA's National Weather Service</credit>
<credit_URL>http://weather.gov/</credit_URL>
<image>
  <url>http://weather.gov/images/xml_logo.gif</url>
  <title>NOAA's National Weather Service</title>
  <link>http://weather.gov</link>
</image>
<location>New York/John F. Kennedy Intl Airport, NY</location>
<station_id>KJFK</station_id>
<latitude>40.66</latitude>
<longitude>-73.78</longitude>
<observation_time_rfc822>Mon, 11 Feb 2008 06:51:00 -0500 EST
</observation_time_rfc822>
<weather>A Few Clouds</weather>
<temp_f>11</temp_f>
<temp_c>-12</temp_c>
<relative_humidity>36</relative_humidity>
<wind_dir>West</wind_dir>
<wind_degrees>280</wind_degrees>
<wind_mph>18.4</wind_mph>
<wind_gust_mph>29</wind_gust_mph>
<pressure_mb>1023.6</pressure_mb>
<pressure_in>30.23</pressure_in>
<dewpoint_f>-11</dewpoint_f>
<dewpoint_c>-24</dewpoint_c>
<windchill_f>-7</windchill_f>
<windchill_c>-22</windchill_c>
<visibility_mi>10.00</visibility_mi>
<icon_url_base>http://weather.gov/weather/images/fcicons/</icon_url_base>
<icon_url_name>nfew.jpg</icon_url_name>
<disclaimer_url>http://weather.gov/disclaimer.html</disclaimer_url>
<copyright_url>http://weather.gov/disclaimer.html</copyright_url>
</current_observation>