jquery - Traversing an XML Nodes using javascript -
i following xml ajax call:
<cars> <car id="spyder"> <year>1946</year> <power>150bhp</power> <description>a classic car smooth lines, rounded lights , 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 smooth curves.</description> <image>2.jpg</image> </car> <car id="career"> <year>1932</year> <power>250bhp</power> <description>a triumph of engineering independent suspension , brakes.</description> <image>3.jpg</image> </car> </cars> i trying fetch information cars. tried accessing firstchild.text etc. latest code tried, i'm still getting exception saying object #<element> has no method 'getfirstchild', presumably there problem function getfirstchild.
could 1 please guide me how can go ahead, traverse , fetch data?
here's code:
for (var = 0; < 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); }
for reason above things did not work. below code worked though..
x=xmldoc.documentelement; y=xmldoc.documentelement.childnodes; var temp1,temp2; (i=0;i<y.length;i++) { if (y[i].nodetype!=3) {carname=y[i].getattribute("id"); (z=0;z<y[i].childnodes.length;z++) { //alert(y[i].childnodes[z].nodename); if (y[i].childnodes[z].nodetype!=3 && y[i].childnodes[z].nodename=="year") { year =y[i].childnodes[z].childnodes[0].nodevalue; } if (y[i].childnodes[z].nodetype!=3 && y[i].childnodes[z].nodename=="power") { power =y[i].childnodes[z].childnodes[0].nodevalue; } if (y[i].childnodes[z].nodetype!=3 && y[i].childnodes[z].nodename=="description") { description =y[i].childnodes[z].childnodes[0].nodevalue; } if (y[i].childnodes[z].nodetype!=3 && y[i].childnodes[z].nodename=="image") { image =y[i].childnodes[z].childnodes[0].nodevalue; alert(image); image="data\\images\\"+image; } } hope helps someone..:-)
Comments
Post a Comment