第一章    XML的介绍和使用

1.1  简介

XML是一种可扩展标记性语言,它主要用于存储和传输数据。它是连接人和机器的一个桥梁。

它的标记可以通过用户自己定义,它被W3C推荐。

XML Example 1




<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>




         这个例子的标记就是用户自己设计,它用来保存发件人和收件人信息,同时它也保存了邮件头和邮件内容,但是这个XML文档没做任何事情,它仅仅是把信息用标签封装起来而已。

  1.1.1 XML 和 HTML 的不同

XML和HTML有不同的设计目的:

l  XML用来存储和传输数据-----------关注的数据内容

l  HTML用来显示数据-------------------关注数据的显示方式

l  XML不用像HTML预定义标签

XML不用预定义标签,就如上面的例子(<to> and<from>),这些标签需要XML的作者自己发明。HTML需要用预定义标签,像<p>, <h1>, <table>等才能工作。然而XML的使用,作者必须自己定义tags和文档结构。

大多数XML应用希望一直能被使用,即使添加和移走数据。试想一下,之前设计的XML Example1,如果一个新版本出现,添加<date> 和 <hour> 元素,移走 <heading>。变成如下XML Example1,老版本的数据将依然能被应用程序使用。




<note>
<date>2015-09-01</date>
<hour>08:30</hour>
<to>Tove</to>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
</note>



 



  1.1.2 XML把事情简单化

XML能使数据分享、数据传输、平台变更、可利用数据简单化。大多数系统包含的数据格式是冲突的,改变两个冲突系统间的数据,对于开发者而言是一个很费时的工作。大量的数据别转化,互斥的数据经常被丢失。

XML用纯文本格式存储数据,这种格式给软硬件之间的数据存储、数据传输以及数据的分享提供了平台。

用XML,数据能被各种读的设备利用,例如人、电脑、声音设备、新的事物等、

  1.1.3 怎么使用XML

 XML大多数被应用在web开发方面和把描述和数据分离方面。

1.      XML把数据和描述分离

XML不承担怎么在页面显示的任何职责,相同的XML数据可以在多个描述场景中使用。由于这,使用XML可以把数据和描述完全分离

2.      XML经常通过HTML实现

在大多数HTML应用程序中,XML用来存储和传输数据,当用HTML格式显示相同的数据时。

3.      XML把数据从HTML中分离出来

当使用HTML来显示数据时,一旦数据库发生变化,作者就不得不去编辑HTML文件。可以使用XML,把数据存储在分离的XML文件中,用几行JavaScrip代码去读XML文件和更新HTML中的数据。



Books.xml



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

<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>

<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>

<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>

</bookstore>





在DOM章节你将学会很多关于XML的使用和JavaScrip。

  1.1.4 事务数据

在大多数不同的领域,成千上万种XML格式其描述一天天的数据事务:

l  股票与证券


l  官方事务


l  医疗数据


l  数学数据


l  科学计量


l  新闻信息


l  天气服务

例如:XML新闻


XML新闻是交换新闻和其他信息的一种策略:用一个标准,这个标准使新闻的生产者和新闻的消费者去生产、接收和把从不同软件、硬件以及程序语言中收集的各种信息信息归档变得更为简单。


XMLNews.xml


<?xml version="1.0" encoding="UTF-8"?>
<nitf>
<head>
<title>ColombiaEarthquake</title>
</head>
<body>
<headline>
<hl1>143 Dead in Colombia Earthquake</hl1>
</headline>
<byline>
<bytag>ByJared Kotler, Associated Press Writer</bytag>
</byline>
<dateline>
<location>Bogota,Colombia</location>
<date>Monday January 25 1999 7:28 ET</date>
</dateline>
</body>
</nitf>



 


例如:天气服务


从NOAA(美国国家海洋和大气句)获取的天气服务的XML.


XMLWeatherService.xml


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

<credit>NOAA'sNational Weather Service</credit>
<credit_URL>http://weather.gov/</credit_URL>

<image>
<url>http://weather.gov/images/xml_logo.gif</url>
<title>NOAA'sNational Weather Service</title>
<link>http://weather.gov</link>
</image>

<location>NewYork/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>AFew 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>







 

1.2  XML树

XML文档是一个树格式是,树结构是从根开始延伸到叶。

XML 入门学习笔记(一)_数据

一个XML文档的例子:如上图描述的books.xml文件:




<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>GiadaDe Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>JK. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">Learning XML</title>
<author>ErikT. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>