0

我已经学习了很多教程,但我似乎仍然无法做到这一点。

我有以下 XML 文档

<?xml version="1.0" encoding="UTF-8"?>

<bookstore>

  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>

  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>

  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>

</bookstore>

我的 HTML 中也有以下 javascript

if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else // for IE 5/6
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET","book.xml",false);
xhttp.send();
xmlDoc=xhttp.responseXML;

alert(xmlDoc.getElementsByTagName("title").nodeValue);

我希望能够提醒特定标题(或所有标题,如果可能的话)。

这怎么可能?

4

1 回答 1

1

那么我怎样才能得到第一个“标题”并提醒它呢?

假设xmlDoc,

var titles = xmlDoc.getElementsByTagName("title"); // NodeList
if (titles[0])                    // if there is an item in index 0
    alert(titles[0].textContent); // alert it's textContent
else                              // otherwise
    alert('Error: no titles');    // some error message
于 2013-10-28T13:35:32.203 回答