2013-11-28 122 views
0

我有一個像XML解析使用javascript

<root> 
<Branch CO_CODE="9" CO_CITY_ST="AHMEDABAD"/> 
<Branch CO_CODE="6" CO_CITY_ST="BANGALORE"/> 
<Branch CO_CODE="4" CO_CITY_ST="DELHI"/> 
<Branch CO_CODE="3" CO_CITY_ST="HYDERABAD"/> 
<Branch CO_CODE="5" CO_CITY_ST="JAIPUR"/> 
<Branch CO_CODE="1" CO_CITY_ST="KOLKATA"/> 
<Branch CO_CODE="8" CO_CITY_ST="LUCKNOW"/> 
<Branch CO_CODE="10" CO_CITY_ST="MUMBAI"/> 
<Branch CO_CODE="2" CO_CITY_ST="SURAT"/> 
<Branch CO_CODE="7" CO_CITY_ST="VARANASI"/> 
</root> 

一個XML我嘗試使用JavaScript解析它,我已經做了現在仍然

$(xml).find("Branch").each(function() 
{ 
    var str = $(this).text() +' '; 
    alert(str); 
}); 

這是給10警報,但我怎麼能顯示CO_CODECO_CITY_ST值?

+0

的可能重複讓它【如何解析XML與jQuery單獨的屬性?](http://stackoverflow.com/questions/5747335/how-to-parse-xml-屬性與 - jQuery的孤獨) – Pointy

回答

2

CO_CODECO_CITY_STBranch標籤的屬性。所以,你可以使用.attr

$(xml).find("Branch").each(function() 
{ 
    var str = $(this).text() +' '; 
    var code = $(this).attr('CO_CODE'); //returns Co_CODE 
    var city = $(this).attr('CO_CITY_ST'); //returns CO_CITY_ST 
    alert(str); 
});