0

我想使用 ajax 从 xml 文件中获取特定数据。

function loadXMLDoc(url)
{
page = 1;
perPage = 3;
content = document.getElementById('bookList');
pagination = document.getElementById('pageUL');
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET",url,false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML; 
    records = xmlDoc.getElementsByTagName("book");
    paganation(1);
 }

这个函数是调用 onload 事件。上述功能正常工作。我的 XML 文件:

<CATALOG>
<book category="A"><book_title>aaaa</book_title><book_desc>aa</book_desc></book>
<book category="B"><book_title>aaaa</book_title><book_desc>aa</book_desc></book>
<book category="A"><book_title>aaaa</book_title><book_desc>aa</book_desc></book>
</CATALOG>

但问题是如何获取特定数据,如类别是“A”或“B”

4

1 回答 1

0

只需在单个节点上调用该getAttribute方法:<book>

/* your loop over records */ {
    var book = records[i];
    var category = book.getAttribute("category");
    // do anything with the data
}
于 2013-08-18T12:33:03.977 回答