2010-06-11 100 views
2

使用JQuery或Javascript如何從'x'屬性的'2'顯示'id'屬性從下面的xml返回'Mary Boone'?通過屬性找到xml元素

我的線沿線的一些思考 -

var result = xml.getElementByAttribute("2").gallery.text(); 

的XML:

<shows> 
    <show id="1"> 
     <artist>Andreas Gursky</artist> 
     <gallery>Matthew Marks</gallery> 
     <medium>photography</medium> 
    </show> 
<show id="2"> 
     <artist>Eric Fischl</artist> 
     <gallery>Mary Boone</gallery> 
     <medium>painting</medium> 
    </show> 
</shows> 

回答

4

使用jQuery,你可以這樣做:

var result = $(xml).find("show[id=2] > gallery").text(); 

如:

$.ajax({ 
    url:'/path/to/file.xml', 
    success:function(xml) { 
     var result = $(xml).find("show[id=2] > gallery").text(); 
     alert(result); 
    } 
}); 

編輯:>添加到選擇器。不需要,但更好一點。

+0

正是我在找的,謝謝! – Moudy 2010-06-11 16:13:58

+0

不客氣。 :O) – user113716 2010-06-11 16:14:22