Traversing an XML Nodes using javascript

saintdark

New Member
I have an XML that i get from an AJAX call to an XML File. The XML that i get is as below.\[code\]<cars><car id="Spyder"><year>1946</year><power>150bhp</power><description>A classic car with smooth lines, rounded lights and recessed exhaust.</description><image>1.jpg</image></car><car id="Solaris"><year>1935</year><power>145bhp</power><description>A revisionist design, encompassing agressive engine lines balanced with smooth curves.</description><image>2.jpg</image></car><car id="Career"><year>1932</year><power>250bhp</power><description>A triumph of engineering with independant suspension and brakes.</description><image>3.jpg</image></car></cars>\[/code\]I am trying to fetch all the information about the cars. I tries some stuff like firstChild.text etc. This was the latest code that i tried. But i still get an exception saying "Object # has no method 'getFirstChild'". So there is some problem with the function getFirstChild. Could some one please guide me as to how i can go ahead,traverse and fetch the data. below is the code that i have written. Thanks all in advance.\[code\]for (var i = 0; i < data.getElementsByTagName("car").length; i++) { carname = data.getElementsByTagName("car").getAttribute("id"); year = data.getElementsByTagName("car").getFirstChild().getTextContent(); power = data.getElementsByTagName("car").getSecondChild().getTextContent(); description = data.getElementsByTagName("car").getThirdChild().getTextContent(); image = data.getElementsByTagName("car").getFourthChild().getTextContent(); alert(carname + year + power + description + image);}\[/code\]
 
Back
Top