我想使用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>
但问题是如何获得类别等特定数据是&#34; A&#34;或&#34; B&#34;
答案 0 :(得分:0)
只需在单个<book>
节点上调用getAttribute
method:
/* your loop over records */ {
var book = records[i];
var category = book.getAttribute("category");
// do anything with the data
}