使用$.ajax()过滤XML文件

Filtering an XML file using $.ajax()

本文关键字:过滤 XML 文件 ajax 使用      更新时间:2023-09-26

我正在使用以下代码查询一个大型(1000个节点)XML文件:

$.ajax({
                        type: "GET",
                        cache: false,
                        url: "someFile.xml",
                        dataType: "xml",
                        contentType: "text/xml",
                        success: function(xmlHttpRequest)

和以下XML结构:

<hospitals>
  <hospital>
    <id>1</id>
    <name>H1</name>
    <city>Riyadh</city>
    <tel>1234567</tel>
    <coordinates>27.034052,49.490662</coordinates>
  </hospital>
</hospitals>

我的问题是:有没有一种方法来过滤(基于城市为例)的XML文件在没有读取整个文件,然后自己过滤?我很确定上面的调用中有一个字段进行过滤,但我无法确定

使用解析XML

var xml = "<hospitals>'
  <hospital>'
    <id>1</id>'
    <name>H1</name>'
    <city>Riyadh</city>'
    <tel>1234567</tel>'
    <coordinates>27.034052,49.490662</coordinates>'
  </hospital>'
</hospitals>",
    xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc ),
        $title = $xml.find( "city" ).text();
if($title=='Riyadh')
    alert($xml.find("tel").text());
http://jsfiddle.net/9JUNK/3/

或者您可以使用dataFilter过滤响应,然后在成功处理程序

中处理它
dataFilter(data, type)Function
A function to be used to handle the raw response data of XMLHttpRequest.
This is a pre-filtering function to sanitize the response.
You should return the sanitized data. The function accepts two arguments:
The raw data returned from the server and the 'dataType' parameter.