XML DOM - 高級

在本教程的較早章節(jié)中,我們介紹了 XML DOM,并使用了 XML DOM 的 getElementsByTagName() 方法從 XML 文檔中取回數(shù)據(jù)。

在本章中我們將結(jié)合一些其他重要的 XML DOM 方法。

您可以在我們的 XML DOM 教程 中學習更多有關(guān) XML DOM 的知識。


獲取元素的值

下面的實例中使用的 XML 文件:books.xml。

下面的實例檢索第一個 <title> 元素的文本值:

實例

<!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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例


獲取屬性的值

下面的實例檢索第一個 <title> 元素的 "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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例


改變元素的值

下面的實例改變第一個 <title> 元素的文本值:

實例

<!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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例


創(chuàng)建新的屬性

XML DOM 的 setAttribute() 方法可用于改變現(xiàn)有的屬性值,或創(chuàng)建一個新的屬性。

下面的實例創(chuàng)建了一個新的屬性(edition="first"),然后把它添加到每一個 <book> 元素中:

實例

<!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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例


創(chuàng)建元素

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> 元素:

實例

<!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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例


實例解釋

  • 創(chuàng)建一個 <edition> 元素

  • 創(chuàng)建值為 "First" 的文本節(jié)點

  • 把這個文本節(jié)點追加到新的 <edition> 元素

  • 把 <edition> 元素追加到第一個 <book> 元素


刪除元素

實例

<!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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例

注釋:上面實例的結(jié)果可能會根據(jù)所用的瀏覽器而不同。Firefox 把新行字符當作空的文本節(jié)點,而 Internet Explorer 不是這樣。您可以在我們的 XML DOM 教程 中閱讀到更多有關(guān)這個問題以及如何避免它的知識。