20161101
XML的聲明
1 <?XML version="1.0" encoding="UTF-8" ?>
XML文檔必須有根元素
XML 對大小寫敏感
所有XML元素必須有關閉標簽
XML文檔必須加引號
在XML中,一些字符擁有特殊的意義,需要實體引用。
XML 中,有 5 個預定義的實體引用:
< < 小於
> > 大於
& & 和號
' ' 單引號
" " 引號
XML的注釋
<!— This is a comment —>
在XML中空格會被保留
盡量使用元素來描述數據,而僅僅使用屬性來提供與數據無關的信息。
元數據應當存儲為屬性(數據的數據),數據本身應當存儲為元素。
XML驗證
http://www.w3school.com.cn/xml/xml_validator.asp
通過測試谷歌浏覽器不能提示錯誤,火狐浏覽器可以提示錯誤
XML Javascript
創建XMLHttpRequest 對象的語法
1 xmlhttp = new XMLHttpRequest();
老版本的internetExplorer (IE5和IE6)使用ActiveX對象
1 xmlhttp = new ActiveXObject(“Microsoft.XMLHTTP”);
實例
TEST.html

1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title></title>
5 <script type="text/javascript">
6 var xmlhttp;
7 function loadXmlDoc(url){
8 if (window.XMLHttpRequest) {
9 xmlhttp = new XMLHttpRequest();
10 }else if(window.ActiveXObject){
11 xmlhttp = new ActiveXObject();
12 }
13 if (xmlhttp != null) {
14 xmlhttp.onreadystatechange = state_change;
15 xmlhttp.open("GET",url,true);
16 xmlhttp.send(null);
17 }else{
18 alert("Your browser does not supprot XMLHTTP.");
19 }
20 }
21 function state_change(){
22 if (xmlhttp.readyState == 4){
23 //4 = "loaded"
24 if (xmlhttp.status == 200) {
25 //200 = "OK"
26 document.getElementById('A1').innerHTML = xmlhttp.status;
27 document.getElementById('A2').innerHTML = xmlhttp.statusText;
28 document.getElementById('A3').innerHTML = xmlhttp.responseText;
29 }else{
30 alert("Problem retrieving XML data:" + xmlhttp.statusText);
31 }
32 }
33 }
34 </script>
35 </head>
36 <body>
37 <h2>Using the HttpRequest Object</h2>
38 <p><b>Status:</b>
39 <span id="A1"></span>
40 </p>
41
42 <p><b>Status text:</b>
43 <span id="A2"></span>
44 </p>
45
46 <p><b>Response:</b>
47 <br /><span id="A3"></span>
48 </p>
49 <button onclick="loadXmlDoc('/note.xml')">Get XML</button>
50 </body>
51 </html>
note.xml

1 <?xml version="1.0" encoding="UTF-8"?> 2 <note> 3 <to>George</to> 4 <from>John</from> 5 <heading>Reminder</heading> 6 <body>Don't forget the meeting!</body> 7 </note>