如何使用ajax从XML文件中检索特定数据?

时间:2021-01-05 14:01:25

I want to fetch the specific data from an xml file using ajax.

我想使用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);
 }

this function is call onload event. this above function work properly. My Xml File:

这个函数是调用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>

but problem is that how to get specific data like category is "A" or "B"

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

1 个解决方案

#1


0  

Just call the getAttribute method on a single <book> node:

只需在单个 节点上调用getAttribute方法:

/* your loop over records */ {
    var book = records[i];
    var category = book.getAttribute("category");
    // do anything with the data
}

#1


0  

Just call the getAttribute method on a single <book> node:

只需在单个 节点上调用getAttribute方法:

/* your loop over records */ {
    var book = records[i];
    var category = book.getAttribute("category");
    // do anything with the data
}