2012-01-04 51 views
1

我使用下面的代碼查詢大(1000個節點)的XML文件的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文件,然後自行過濾?我很確定在上面的調用中有一個字段進行過濾,但我無法弄清楚。

回答

0

使用parse 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.