?
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
在本教程的較早章節(jié)中,我們介紹了 XML DOM,并使用了 XML DOM 的 getElementsByTagName() 方法從 XML 文檔中取回數(shù)據(jù)。
在本章中我們將結(jié)石一些其他重要的 XML DOM 方法。
您可以在我們的 XML DOM 教程 中學(xué)習(xí)更多有關(guān) XML DOM 的知識。
下面的實例中使用的 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>
下面的實例檢索第一個 <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
下面的實例檢索第一個 <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
下面的實例改變第一個 <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)建一個新的屬性。
下面的實例創(chuàng)建了一個新的屬性(edition="first"),然后把它添加到每一個 <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)建一個新的元素節(jié)點。
XML DOM 的 createTextNode() 方法創(chuàng)建一個新的文本節(jié)點。
XML DOM 的 appendChild() 方法向節(jié)點添加子節(jié)點(在最后一個子節(jié)點之后)。
如需創(chuàng)建帶有文本內(nèi)容的新元素,需要同時創(chuàng)建元一個新的元素節(jié)點和一個新的文本節(jié)點,然后把他追加到現(xiàn)有的節(jié)點。
下面的實例創(chuàng)建了一個新的元素(<edition>),帶有如下文本:First,然后把它添加到第一個 <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
實例解釋
創(chuàng)建一個 <edition> 元素
創(chuàng)建值為 "First" 的文本節(jié)點
把這個文本節(jié)點追加到新的 <edition> 元素
把 <edition> 元素追加到第一個 <book> 元素
下面的實例刪除第一個 <book> 元素的第一個節(jié)點:
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
注釋:上面實例的結(jié)果可能會根據(jù)所用的瀏覽器而不同。Firefox 把新行字符當作空的文本節(jié)點,而 Internet Explorer 不是這樣。您可以在我們的 XML DOM 教程 中閱讀到更多有關(guān)這個問題以及如何避免它的知識。