XML DOM - 高級
在本教程的較早章節(jié)中,我們介紹了 XML DOM,并使用了 XML DOM 的 getElementsByTagName() 方法從 XML 文檔中取回?cái)?shù)據(jù)。
在本章中我們將結(jié)合一些其他重要的 XML DOM 方法。
您可以在我們的 XML DOM 教程 中學(xué)習(xí)更多有關(guān) XML DOM 的知識。
獲取元素的值
下面的實(shí)例中使用的 XML 文件:books.xml。
下面的實(shí)例檢索第一個 <title> 元素的文本值:
實(shí)例
<!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>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
獲取屬性的值
下面的實(shí)例檢索第一個 <title> 元素的 "lang" 屬性的文本值:
實(shí)例
<!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>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
改變元素的值
下面的實(shí)例改變第一個 <title> 元素的文本值:
實(shí)例
<!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("title")[0].childNodes[0]; x.nodeValue="Easy Cooking"; x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; txt=x.nodeValue; document.write(txt); </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
創(chuàng)建新的屬性
XML DOM 的 setAttribute() 方法可用于改變現(xiàn)有的屬性值,或創(chuàng)建一個新的屬性。
下面的實(shí)例創(chuàng)建了一個新的屬性(edition="first"),然后把它添加到每一個 <book> 元素中:
實(shí)例
<!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 values for (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>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
創(chuàng)建元素
XML DOM 的 createElement() 方法創(chuàng)建一個新的元素節(jié)點(diǎn)。
XML DOM 的 createTextNode() 方法創(chuàng)建一個新的文本節(jié)點(diǎn)。
XML DOM 的 appendChild() 方法向節(jié)點(diǎn)添加子節(jié)點(diǎn)(在最后一個子節(jié)點(diǎn)之后)。
如需創(chuàng)建帶有文本內(nèi)容的新元素,需要同時創(chuàng)建元一個新的元素節(jié)點(diǎn)和一個新的文本節(jié)點(diǎn),然后把他追加到現(xiàn)有的節(jié)點(diǎn)。
下面的實(shí)例創(chuàng)建了一個新的元素(<edition>),帶有如下文本:First,然后把它添加到第一個 <book> 元素:
實(shí)例
<!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>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
實(shí)例解釋
創(chuàng)建一個 <edition> 元素
創(chuàng)建值為 "First" 的文本節(jié)點(diǎn)
把這個文本節(jié)點(diǎn)追加到新的 <edition> 元素
把 <edition> 元素追加到第一個 <book> 元素
刪除元素
實(shí)例
<!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>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
注釋:上面實(shí)例的結(jié)果可能會根據(jù)所用的瀏覽器而不同。Firefox 把新行字符當(dāng)作空的文本節(jié)點(diǎn),而 Internet Explorer 不是這樣。您可以在我們的 XML DOM 教程 中閱讀到更多有關(guān)這個問題以及如何避免它的知識。