XML是存儲(chǔ)和透過因特網(wǎng)傳輸結(jié)構(gòu)化數(shù)據(jù)的標(biāo)準(zhǔn)
1:可擴(kuò)展標(biāo)記語言
2:被設(shè)計(jì)用來傳輸和存儲(chǔ)數(shù)據(jù)
3:只要會(huì)html XML就好學(xué)2者相同有不同
4:一個(gè)是用在格式化顯示數(shù)據(jù) 一個(gè)是用來數(shù)據(jù)交互的
5:html一個(gè)標(biāo)記語言預(yù)定義標(biāo)簽的 xml則是一個(gè)可擴(kuò)展的自定義標(biāo)記語言
JS中創(chuàng)建一個(gè)xml //命名空間 節(jié)點(diǎn) doctype
eg: var xml=document.implementation.createDocument("","root",null)
alert(xml.documentElement.tagName)//root
var child= xml.createElement("child“) //類似dom 的創(chuàng)建追加一個(gè)節(jié)點(diǎn)
xml.documentElement.appendChild(child)
創(chuàng)建外部文件
<? version="1.0" encoded="utf-8"?>
申明編碼 版本
<root>
<name money="much">曹士遠(yuǎn)</name>
<age>23</age>
</root>
root是一個(gè)根標(biāo)簽節(jié)點(diǎn) 類似于html 但是必須是雙標(biāo)簽區(qū)分大小寫 可寫屬性
XML 語法規(guī)則
XML 文檔必須有根元素
類似上面最外層的root
XML 聲明
第一行加上<?xml version="1.0" encoding="utf-8"?>
注意:所有的 XML 元素都必須有一個(gè)關(guān)閉標(biāo)簽,XML 標(biāo)簽對(duì)大小寫敏感恃慧,XML 必須正確嵌套筷屡,XML 屬性值必須加引號(hào),在 XML 中泻红,空格會(huì)被保留
實(shí)體引用
如果您把字符 "<" 放在 XML 元素中谓着,會(huì)發(fā)生錯(cuò)誤渡讼,這是因?yàn)榻馕銎鲿?huì)把它當(dāng)作新元素的開始。
error eg:<message>if salary < 1000 then</message>
eg:<message>if salary < 1000 then</message>
< < less than
> > greater than
& & ampersand
' ' apostrophe
" " quotation mark
注釋
XML 以 LF 存儲(chǔ)換行
在 Windows 應(yīng)用程序中俭令,換行通常以一對(duì)字符來存儲(chǔ):回車符(CR)和換行符(LF)后德。
在 Unix 和 Mac OSX 中,使用 LF 來存儲(chǔ)新行抄腔。
在舊的 Mac 系統(tǒng)中瓢湃,使用 CR 來存儲(chǔ)新行。
XML 以 LF 存儲(chǔ)換行赫蛇。
基本語法和HTML類似詳情請查看http://www.runoob.com/xml/xml-applications.html菜鳥教程
CDATA這
XML 文檔中的所有文本均會(huì)被解析器解析绵患。
只有 CDATA 區(qū)段中的文本會(huì)被解析器忽略。
<script>
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1;
}
else
{
return 0;
}
}
]]>
</script>
在上面的實(shí)例中悟耘,解析器會(huì)忽略 CDATA 部分中的所有內(nèi)容落蝙。
比如 JavaScript 代碼,包含大量 "<" 或 "&" 字符暂幼。為了避免錯(cuò)誤筏勒,可以將腳本代碼定義為 CDATA。
xml服務(wù)器
通過 ASP 生成 XML
XML 可在不安裝任何 XML 軟件的情況下在服務(wù)器端生成旺嬉。
如需從服務(wù)器生成 XML 響應(yīng) - 只需簡單地編寫以下代碼并在 Web 服務(wù)器上把它保存為一個(gè) ASP 文件:
<%
response.ContentType="text/xml"
response.Write("<?xml version='1.0' encoding='ISO-8859-1'?>")
response.Write("<note>")
response.Write("<from>Jani</from>")
response.Write("<to>Tove</to>")
response.Write("<message>Remember me this weekend</message>")
response.Write("</note>")
%>
通過 PHP 生成 XML
如需使用 PHP 從服務(wù)器上生成 XML 響應(yīng)管行,請使用下面的代碼:
<?php
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='ISO-8859-1'?>";
echo "<note>";
echo "<from>Jani</from>";
echo "<to>Tove</to>";
echo "<message>Remember me this weekend</message>";
echo "</note>";
?>
請注意,響應(yīng)頭部的內(nèi)容類型必須設(shè)置為 "text/xml"邪媳。
從數(shù)據(jù)庫生成 XML
XML 可在不安裝任何 XML 軟件的情況下從數(shù)據(jù)庫生成捐顷。
如需從服務(wù)器生成 XML 數(shù)據(jù)庫響應(yīng)荡陷,只需簡單地編寫以下代碼,并把它在 Web 服務(wù)器上保存為 ASP 文件:
<%
response.ContentType = "text/xml"
set conn=Server.CreateObject("ADODB.Connection")
conn.provider="Microsoft.Jet.OLEDB.4.0;"
conn.open server.mappath("/db/database.mdb")
sql="select fname,lname from tblGuestBook"
set rs=Conn.Execute(sql)
response.write("<?xml version='1.0' encoding='ISO-8859-1'?>")
response.write("<guestbook>")
while (not rs.EOF)
response.write("<guest>")
response.write("<fname>" & rs("fname") & "</fname>")
response.write("<lname>" & rs("lname") & "</lname>")
response.write("</guest>")
rs.MoveNext()
wend
rs.close()
conn.close()
response.write("</guestbook>")
%>
其實(shí)還是這個(gè)主要XML DOM 高級(jí)(前端嗎)
下面的實(shí)例中使用的 XML 文件:books.xml迅涮。
下面的實(shí)例檢索第一個(gè) <title> 元素的文本值:
實(shí)例
txt=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
下面的實(shí)例檢索第一個(gè) <title> 元素的 "lang" 屬性的文本值:
實(shí)例
txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");
filename=tryxml_dom_getattribute)
改變元素的值
下面的實(shí)例改變第一個(gè) <title> 元素的文本值:
實(shí)例
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";
創(chuàng)建新的屬性
XML DOM 的 setAttribute() 方法可用于改變現(xiàn)有的屬性值废赞,或創(chuàng)建一個(gè)新的屬性。
下面的實(shí)例創(chuàng)建了一個(gè)新的屬性(edition="first")叮姑,然后把它添加到每一個(gè) <book> 元素中:
實(shí)例
x=xmlDoc.getElementsByTagName("book");
for(i=0;i<x.length;i++)
{
x[i].setAttribute("edition","first");
}
創(chuàng)建元素
XML DOM 的 createElement() 方法創(chuàng)建一個(gè)新的元素節(jié)點(diǎn)唉地。
XML DOM 的 createTextNode() 方法創(chuàng)建一個(gè)新的文本節(jié)點(diǎn)。
XML DOM 的 appendChild() 方法向節(jié)點(diǎn)添加子節(jié)點(diǎn)(在最后一個(gè)子節(jié)點(diǎn)之后)戏溺。
如需創(chuàng)建帶有文本內(nèi)容的新元素渣蜗,需要同時(shí)創(chuàng)建元一個(gè)新的元素節(jié)點(diǎn)和一個(gè)新的文本節(jié)點(diǎn)屠尊,然后把他追加到現(xiàn)有的節(jié)點(diǎn)旷祸。
下面的實(shí)例創(chuàng)建了一個(gè)新的元素(<edition>),帶有如下文本:First讼昆,然后把它添加到第一個(gè) <book> 元素:
實(shí)例
newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("First");
newel.appendChild(newtext);
x=xmlDoc.getElementsByTagName("book");
x[0].appendChild(newel);
刪除元素
下面的實(shí)例刪除第一個(gè) <book> 元素的第一個(gè)節(jié)點(diǎn):
實(shí)例
x=xmlDoc.getElementsByTagName("book")[0];
x.removeChild(x.childNodes[0])
XML 數(shù)據(jù)島
它是什么托享?XML 數(shù)據(jù)島是嵌入到 HTML 頁面中的 XML 數(shù)據(jù)。
為什么要避免使用它浸赫?XML 數(shù)據(jù)島只在 Internet Explorer 瀏覽器中有效闰围。
用什么代替它?您應(yīng)當(dāng)在 HTML 中使用 JavaScript 和 XML DOM 來解析并顯示 XML既峡。
如需更多有關(guān) JavaScript 和 XML DOM 的信息羡榴,請?jiān)L問 XML DOM 教程。
XML 數(shù)據(jù)島實(shí)例
本例使用 XML 文檔 "cd_catalog.xml"运敢。
把 XML 文檔綁定到 HTML 文檔中的一個(gè) <xml> 標(biāo)簽校仑。id 屬性定義數(shù)據(jù)島的標(biāo)識(shí)符,而 src 屬性指向 XML 文件:
實(shí)例
本實(shí)例只適用于 IE 瀏覽器
<html><body><xml id="cdcat" src="cd_catalog.xml"></xml><table border="1" datasrc="#cdcat"><tr><td><span datafld="ARTIST"></span></td><td><span datafld="TITLE"></span></td></tr></table></body></html>
現(xiàn)實(shí)實(shí)例:
XML 氣象服務(wù)
XML 國家氣象服務(wù)案例传惠,來自 NOAA(National Oceanic and Atmospheric Administration):
<?xml version="1.0" encoding="ISO-8859-1" ?>
<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>