2011-06-07 168 views
0

我有一個奇怪的XML文件,我試圖解析。XML jquery解析幫助

<Data> 
<Row> 
<Field name = "whatever" pos ="1">STUFF</Field> 
<Field name = "whatever2" pos ="2">MORE STUFF</Field> 
<Field name = "whatever3" pos ="3">EVEN MORE STUFF</Field> 
</Row> 
<Row> 
<Field name = "whatever" pos ="1">Different STUFF</Field> 
<Field name = "whatever2" pos ="2">MORE Different STUFF</Field> 
<Field name = "whatever3" pos ="3">EVEN MORE Different STUFF</Field> 
</Row> 
</Data> 

我試圖通過使用「行」的工作,但如何調用單個字段名稱獲取數據?在螢火蟲中,它們全都看起來像「田野」。我試過了:

myvalue : $("whatever", this).text() 

但是這並沒有抓住它。

感謝


從下面留言代碼:

function callAjax(url) { 
    $.ajax({ 
     url: url, 
     dataType: "xml", 
     success: function (xmlResponse) { 
      $.merge(data1, $("ROW", xmlResponse).map(returnResults).get()); 
     } // end of success }); } 

function returnResults() { 
    formatedURL = $('Field[name="EL_VALUES_FIELD2"]', this).text(); 
    return { 
     value: $('Field[name="EL_VALUES_FIELD1"]', this).text(), 
     label: $('Field[name="EL_VALUES_FIELD1"]', this).text() + " " + $("EL_VALUES_FIELD4", this).text(), 
     title: $('Field[name="EL_VALUES_FIELD1"]', this).text(), 
     url: formatedURL, 
     description: $('Field[name="EL_VALUES_FIELD3"]', this).text(), 
     keywords: $('Field[name="EL_VALUES_FIELD4"]', this).text() 
    }; 
} 

回答

1

您可以用attribute equals selector [attrname="value"]做到這一點。例如:

$('Row', this).eq(0).find('Field[name="whatever2"]').text() 

此發現Field元件,其名稱是whatever2第一(索引0)元件內。

+0

這對XML是否真的有用? – Niklas 2011-06-07 17:24:55

+0

@Niklas是的。 [jsFiddle](http://jsfiddle.net/3vfQ8/) – lonesomeday 2011-06-07 17:27:55

+0

有沒有辦法做到這一點沒有索引?我正在將所有這些編譯到自動完成搜索欄的對象中。我正在處理這個問題: – Specked 2011-06-07 17:47:21