Siddharth M
Siddharth M

Reputation: 3

XML Parsing - jQuery

I writing a small code to parse an XML in jQuery but one only character is being read from the info attribute.

XML:

<series srss="29">
<srs info="2300,Sheffield Shield 2014-15"/>
<srs info="2313,Ram Slam T20 Challenge 2014-15"/>
<srs info="2275,Pakistan vs New Zealand in UAE, 2014"/>
<srs info="2293,England tour of Sri Lanka, 2014"/>
<srs info="2310,Ranji Trophy, 2014-15"/>
<srs info="2278,India tour of Australia, 2014-15"/>
<srs info="2262,West Indies in South Africa, 2014-15"/>
<srs info="2302,Big Bash League 2014-15"/>
<srs info="2291,Sri Lanka tour of New Zealand, 2014-15"/>
<srs info="2279,India and England in Australia Tri-Series, 2015"/>
<srs info="2292,Pakistan tour of New Zealand, 2015"/>
<srs info="2325,WC Warm up matches, 2015"/>
<srs info="2223,WC 2015"/>
<srs info="2307,Irani Cup, 2015"/>
<srs info="2321,County Championship Division One 2015"/>
<srs info="2322,County Championship Division Two 2015"/>
<srs info="2320,England tour of West Indies, 2015"/>
<srs info="2323,NatWest t20 Blast, 2015"/>
<srs info="2284,New Zealand tour of England, 2015"/>
<srs info="2280,India tour of Bangladesh, 2015"/>
<srs info="2285,The Ashes, 2015"/>
<srs info="2324,Royal London One-Day Cup, 2015"/>
<srs info="2286,Australia tour of Ireland, 2015"/>
<srs info="2287,Australia tour of England, 2015"/>
<srs info="2281,T20 WC League, 2015"/>
<srs info="2282,South Africa tour of India, 2015"/>
<srs info="2283,Sri Lanka tour of India, 2015-16"/>
<srs info="2317,England tour of South Africa, 2015-16"/>
<srs info="2263,T20 WC 2016"/>
</series>

Code:

             var i=0;
             seriesArray = $(text).find('srs').attr('info'); //Returns 2300,Sheffield Shield 2014-15
             //console.log(seriesArray); 
             var SeriesVal = $(text).find('srs').each(function(){
                //console.log(this);
                seriesArray[i] = $(this).attr('info'); //Returns 2 in first iteration, 3 in second iteration and so on
                //console.log("Value = "+seriesArray[i]);
                i=i+1;
             });

Been stuck with this, might be something really small too. Can someone help. Thanks in advance

Upvotes: 0

Views: 29

Answers (1)

bowheart
bowheart

Reputation: 4676

You're not declaring seriesArray as an array. It's just getting set to that first string, and then you're accessing the parts of that string, and not creating an array. Instead, use

var seriesArray = [];
seriesArray.push($(text).find('srs').attr('info')); //Returns 2300,Sheffield Shield 2014-15

Here's a JSFiddle

Upvotes: 1

Related Questions