XML Parsing with Javascript Display .childNodes -
xml parsing javascript display .childnodes
i studying xml & javascript have problems display .childnodes , .nodevalue of , in file xml.
- search in w3schools.com , google unsuccessfully :(
- i not understand why not display data...
any welcome!
thanks in advance support!
the code xml:
<team id="burnley"> <team_name>burnley</team_name> <description>fundado en 1882. premierleague 2013/14</description> <city>londres </city> <stadium>burnley </stadium> <players> <person> <first_name>daniel johnson</first_name> <country_birth>inglaterra</country_birth> <position>md</position> </person> <person> <first_name>charles n'zogbia</first_name> <country_birth>francia</country_birth> <position>md</position> </person> </players> <image> <src>images/xmleague/burnley.png</src> <title>fc burnley</title> </image> </team>
the code javascript
<script> if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.open("get","xmleague.xml",false); xmlhttp.send(); xmldoc=xmlhttp.responsexml; x=xmldoc.getelementsbytagname("team"); function displayxmleague(i) { team_name=(x[i].getelementsbytagname("team_name")[0].childnodes[0].nodevalue); description=(x[i].getelementsbytagname("description")[0].childnodes[0].nodevalue); city=(x[i].getelementsbytagname("city")[0].childnodes[0].nodevalue); stadium=(x[i].getelementsbytagname("stadium")[0].childnodes[0].nodevalue); players=(x[i].getelementsbytagname("players")[0].childnodes[0].nodevalue); image=(x[i].getelementsbytagname("image")[0].childnodes[0].nodevalue); txt="team_name: "+team_name+"<br>description: "+description+"<br>city: "+city+"<br>stadium: "+stadium+"<br>players: "+players+"<br>image: "+image ; document.getelementbyid("showxmleague").innerhtml=txt; } </script>
anexo: url imagen of test studing
thanks in advance support
in ie getting: unable property 'getelementsbytagname' of undefined or null reference. "x" inside displayxmleague(i) undefined, changed "var x". assume calling "displayxmleague" function. in example below, adding call bottom: "displayxmleague(0);"
<div id="showxmleague" /> <script type="text/javascript"> if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp = new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp = new activexobject("microsoft.xmlhttp"); } xmlhttp.open("get", "xmleague.xml", false); xmlhttp.send(); xmldoc = xmlhttp.responsexml; var x = xmldoc.getelementsbytagname("team"); function displayxmleague(i) { var leagueelement = x[i]; team_name = (leagueelement.getelementsbytagname("team_name")[0].childnodes[0].nodevalue); description = (leagueelement.getelementsbytagname("description")[0].childnodes[0].nodevalue); city = (leagueelement.getelementsbytagname("city")[0].childnodes[0].nodevalue); stadium = (leagueelement.getelementsbytagname("stadium")[0].childnodes[0].nodevalue); var players = leagueelement.getelementsbytagname("first_name"); var player; var playerstring; for(var index = 0; index < players.length; index++) { player = players[index]; if (index == 0) { playerstring = player.childnodes[0].nodevalue; } else { playerstring = (playerstring + "<br />" + player.childnodes[0].nodevalue); } } players = playerstring; image = (leagueelement.getelementsbytagname("image")[0].childnodes[1].childnodes[0].nodevalue); txt = "team_name: " + team_name + "<br>description: " + description + "<br>city: " + city + "<br>stadium: " + stadium + "<br>players: " + players + "<br>image: " + image; document.getelementbyid("showxmleague").innerhtml = txt; } displayxmleague(0); </script>
your output:
team_name: burnley
description: fundado en 1882. premierleague 2013/14
city: londres
stadium: burnley
players: daniel johnson
charles n'zogbia
image: images/xmleague/burnley.png
Comments
Post a Comment