XML(三),xml

來源:互聯網
上載者:User

XML(三),xml
使用 XSLT 顯示 XML 

 

--------------------------------------------------------------------------------

 

通過使用 XSLT,您可以把 XML 文檔轉換成 HTML 格式。

 

 

--------------------------------------------------------------------------------

 

使用 XSLT 顯示 XML

XSLT 是首選的 XML 樣式表語言。

 

XSLT(eXtensible Stylesheet Language Transformations)遠比 CSS 更加完善。

 

XSLT 是在瀏覽器顯示 XML 檔案之前,先把它轉換為 HTML:

 

使用 XSLT 顯示 XML

 

 

 

--------------------------------------------------------------------------------

 

在伺服器上通過 XSLT 轉換 XML

在上面的執行個體中,當瀏覽器讀取 XML 檔案時,XSLT 轉換是由瀏覽器完成的。

 

在使用 XSLT 來轉換 XML 時,不同的瀏覽器可能會產生不同結果。為了減少這種問題,可以在伺服器上進行 XSLT 轉換。

如果您想要學習有關 XSLT 的知識,請在我們的首頁尋找 XSLT 教程。

 

 

 

 

XMLHttpRequest 對象

 

--------------------------------------------------------------------------------

 

XMLHttpRequest 對象

XMLHttpRequest 對象用於在後台與伺服器交換資料。

 

XMLHttpRequest 對象是開發人員的夢想,因為您能夠:

 

在不重新載入頁面的情況下更新網頁 

在頁面已載入後從伺服器請求資料 

在頁面已載入後從伺服器接收資料 

在後台向伺服器發送資料 

建立一個 XMLHttpRequest 對象

所有現代瀏覽器(IE7+、Firefox、Chrome、Safari 和 Opera)都有內建的 XMLHttpRequest 對象。

 

建立 XMLHttpRequest 對象的文法:

 

xmlhttp=new XMLHttpRequest();

舊版本的Internet Explorer(IE5和IE6)中使用 ActiveX 對象:

 

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

XML Parser 

 

--------------------------------------------------------------------------------

 

所有現代瀏覽器都有內建的 XML 解析器。

 

XML 解析器把 XML 文檔轉換為 XML DOM 對象 - 可通過 JavaScript 操作的對象。

 

 

--------------------------------------------------------------------------------

 

解析 XML 文檔

下面的程式碼片段把 XML 文檔解析到 XML DOM 對象中:

 

if (window.XMLHttpRequest)

{// code for IE7+, Firefox, Chrome, Opera, Safari

xmlhttp=new XMLHttpRequest();

}

else

{// code for IE6, IE5

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

}

xmlhttp.open("GET","books.xml",false);

xmlhttp.send();

xmlDoc=xmlhttp.responseXML; 

 

--------------------------------------------------------------------------------

 

解析 XML 字串

下面的程式碼片段把 XML 字串解析到 XML DOM 對象中:

 

txt="<bookstore><book>";

txt=txt+"<title>Everyday Italian</title>";

txt=txt+"<author>Giada De Laurentiis</author>";

txt=txt+"<year>2005</year>";

txt=txt+"</book></bookstore>";

 

if (window.DOMParser)

{

parser=new DOMParser();

xmlDoc=parser.parseFromString(txt,"text/xml");

}

else // Internet Explorer

{

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

xmlDoc.async=false;

xmlDoc.loadXML(txt); 

} 

注釋:Internet Explorer 使用 loadXML() 方法來解析 XML 字串,而其他瀏覽器使用 DOMParser 對象。

 

 

--------------------------------------------------------------------------------

 

跨域訪問

出於安全方面的原因,現代的瀏覽器不允許跨域的訪問。

 

這意味著,網頁以及它試圖載入的 XML 檔案,都必須位於相同的伺服器上。

 

XML DOM 

 

--------------------------------------------------------------------------------

 

DOM(Document Object Model 文件物件模型)定義了訪問和操作文檔的標準方法。

 

 

--------------------------------------------------------------------------------

 

XML DOM

XML DOM(XML Document Object Model)定義了訪問和操作 XML 文檔的標準方法。 

 

XML DOM 把 XML 文檔作為樹結構來查看。

 

所有元素可以通過 DOM 樹來訪問。可以修改或刪除它們的內容,並建立新的元素。元素,它們的文本,以及它們的屬性,都被認為是節點。 

 

 

 

--------------------------------------------------------------------------------

 

HTML DOM

HTML DOM 定義了訪問和操作 HTML 文檔的標準方法。 

 

所有 HTML 元素可以通過 HTML DOM 來訪問。

 

 

 

--------------------------------------------------------------------------------

 

載入一個 XML 檔案 - 跨瀏覽器執行個體

下面的執行個體把 XML 文檔("note.xml")解析到 XML DOM 對象中,然後通過 JavaScript 提取一些資訊:

 

執行個體

<html>

<body>

<h1>W3Schools Internal Note</h1>

<div>

<b>To:</b> <span id="to"></span><br />

<b>From:</b> <span id="from"></span><br />

<b>Message:</b> <span id="message"></span>

</div>

 

<script>

if (window.XMLHttpRequest)

{// code for IE7+, Firefox, Chrome, Opera, Safari

xmlhttp=new XMLHttpRequest();

}

else

{// code for IE6, IE5

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

}

xmlhttp.open("GET","note.xml",false);

xmlhttp.send();

xmlDoc=xmlhttp.responseXML;

 

document.getElementById("to").innerHTML=

xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;

document.getElementById("from").innerHTML=

xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;

document.getElementById("message").innerHTML=

xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;

</script>

 

</body>

</html>

 

--------------------------------------------------------------------------------

 

重要注釋!

如需從上面的 XML 檔案("note.xml")的 <to> 元素中提取文本 "Tove",文法是:

 

getElementsByTagName("to")[0].childNodes[0].nodeValue

請注意,即使 XML 檔案只包含一個 <to> 元素,您仍然必須指定數組索引 [0]。這是因為 getElementsByTagName() 方法返回一個數組。

 

 

--------------------------------------------------------------------------------

 

載入一個 XML 字串 - 跨瀏覽器執行個體

下面的執行個體把 XML 字串解析到 XML DOM 對象中,然後通過 JavaScript 提取一些資訊:

 

執行個體

<html>

<body>

<h1>W3Schools Internal Note</h1>

<div>

<b>To:</b> <span id="to"></span><br />

<b>From:</b> <span id="from"></span><br />

<b>Message:</b> <span id="message"></span>

</div>

 

<script>

txt="<note>";

txt=txt+"<to>Tove</to>";

txt=txt+"<from>Jani</from>";

txt=txt+"<heading>Reminder</heading>";

txt=txt+"<body>Don't forget me this weekend!</body>";

txt=txt+"</note>";

 

if (window.DOMParser)

{

parser=new DOMParser();

xmlDoc=parser.parseFromString(txt,"text/xml");

}

else // Internet Explorer

{

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

xmlDoc.async=false;

xmlDoc.loadXML(txt);

}

 

document.getElementById("to").innerHTML=

xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;

document.getElementById("from").innerHTML=

xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;

document.getElementById("message").innerHTML=

xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;

</script>

</body>

</html> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.