?
本文檔使用 php中文網(wǎng)手冊(cè) 發(fā)布
在本教程的較早章節(jié)中,我們介紹了 XML DOM,并使用了 XML DOM 的 getElementsByTagName() 方法從 XML 文檔中取回?cái)?shù)據(jù)。
在本章中我們將結(jié)石一些其他重要的 XML DOM 方法。
您可以在我們的 XML DOM 教程 中學(xué)習(xí)更多有關(guān) XML DOM 的知識(shí)。
下面的實(shí)例中使用的 XML 文件:books.xml。
<?xml version="1.0" encoding="utf-8"?> <bookstore> <book category="COOKING"> <title>Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title>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"> <title>Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
下面的實(shí)例檢索第一個(gè) <title> 元素的文本值:
txt=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue; <!DOCTYPE html> <html> <body> <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","books.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML; txt=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue; document.write(txt); </script> </body> </html> Everyday Italian
下面的實(shí)例檢索第一個(gè) <title> 元素的 "lang" 屬性的文本值:
txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");
<!DOCTYPE html>
<html>
<body>
<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","books.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");
document.write(txt);
</script>
</body>
</html>
en
下面的實(shí)例改變第一個(gè) <title> 元素的文本值:
txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang"); <!DOCTYPE html> <html> <body> <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","books.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML; txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang"); document.write(txt); </script> </body> </html> en
XML DOM 的 setAttribute() 方法可用于改變現(xiàn)有的屬性值,或創(chuàng)建一個(gè)新的屬性。
下面的實(shí)例創(chuàng)建了一個(gè)新的屬性(edition="first"),然后把它添加到每一個(gè) <book> 元素中:
x=xmlDoc.getElementsByTagName("book"); for(i=0;i<x.length;i++) { x[i].setAttribute("edition","first"); }
<!DOCTYPE html> <html> <body> <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","books.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML; x=xmlDoc.getElementsByTagName("book");for(i=0;i<x.length;i++) { x[i].setAttribute("edition","first"); } //Output all attribute valuesfor (i=0;i<x.length;i++){document.write("Category: " + x[i].getAttribute('category') + "<br>");document.write("Edition: " + x[i].getAttribute('edition') + "<br>"); } </script> </body> </html>
Category: cookingEdition: firstCategory: childrenEdition: firstCategory: webEdition: firstCategory: webEdition: first
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> 元素:
newel=xmlDoc.createElement("edition"); newtext=xmlDoc.createTextNode("First"); newel.appendChild(newtext); x=xmlDoc.getElementsByTagName("book"); x[0].appendChild(newel);
<!DOCTYPE html> <html> <body> <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","books.xml",false);xmlhttp.send();xmlDoc=xmlhttp.responseXML; newel=xmlDoc.createElement("edition");newtext=xmlDoc.createTextNode("First"); newel.appendChild(newtext); x=xmlDoc.getElementsByTagName("book");x[0].appendChild(newel); for (i=0;i<x[0].childNodes.length;i++){if (x[0].childNodes[i].nodeType==1) { document.write(x[0].childNodes[i].nodeName); document.write(": "); document.write(x[0].childNodes[i].childNodes[0].nodeValue); document.write("<br>"); }} </script ></body> </html>
title: Everyday Italianauthor: Giada De Laurentiisyear: 2005price: 30.00edition: First
實(shí)例解釋
創(chuàng)建一個(gè) <edition> 元素
創(chuàng)建值為 "First" 的文本節(jié)點(diǎn)
把這個(gè)文本節(jié)點(diǎn)追加到新的 <edition> 元素
把 <edition> 元素追加到第一個(gè) <book> 元素
下面的實(shí)例刪除第一個(gè) <book> 元素的第一個(gè)節(jié)點(diǎn):
x=xmlDoc.getElementsByTagName("book")[0]; x.removeChild(x.childNodes[0]);
<!DOCTYPE html> <html> <body> <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","books.xml",false);xmlhttp.send(); xmlDoc=xmlhttp.responseXML; var x=xmlDoc.getElementsByTagName("book")[0]; document.write("Child nodes before removal: "); document.write(x.childNodes.length);x.removeChild(x.childNodes[0]); document.write("<br>Child nodes after removal: "); document.write(x.childNodes.length); </script> </body> </html>
Child nodes before removal: 9Child nodes after removal: 8
注釋:上面實(shí)例的結(jié)果可能會(huì)根據(jù)所用的瀏覽器而不同。Firefox 把新行字符當(dāng)作空的文本節(jié)點(diǎn),而 Internet Explorer 不是這樣。您可以在我們的 XML DOM 教程 中閱讀到更多有關(guān)這個(gè)問(wèn)題以及如何避免它的知識(shí)。