I get the following XML from an AJAX call:
<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 aggressive 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 independent suspension and brakes.</description>
<image>3.jpg</image>
</car>
</cars>
I am trying to fetch all the information about the cars. I tried accessing firstChild.text etc. This was the latest code that I tried, but I'm still getting an exception saying Object #<Element> has no method 'getFirstChild', so presumably 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?
Here's my code:
for (var i = 0; i < data.getElementsByTagName("car").length; i++) {
carname = data.getElementsByTagName("car")[i].getAttribute("id");
year = data.getElementsByTagName("car")[i].getFirstChild().getTextContent();
power = data.getElementsByTagName("car")[i].getSecondChild().getTextContent();
description = data.getElementsByTagName("car")[i].getThirdChild().getTextContent();
image = data.getElementsByTagName("car")[i].getFourthChild().getTextContent();
alert(carname + year + power + description + image);
}