0

I have a XML like

<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>

I am try to parsing it using javascript I have done still now

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

It is giving 10 alert but how can I show CO_CODE and CO_CITY_ST value?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
A J
  • 4,542
  • 5
  • 50
  • 80
  • possible duplicate of [How to parse xml attributes with jQuery alone?](http://stackoverflow.com/questions/5747335/how-to-parse-xml-attributes-with-jquery-alone) – Pointy Nov 28 '13 at 13:22

1 Answers1

2

CO_CODE and CO_CITY_ST are attributes of Branch tag. So you can get it using .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);
});
Praveen
  • 55,303
  • 33
  • 133
  • 164