使用Javascript显示XML解析.childNodes

时间:2015-07-02 04:18:40

标签: javascript xml

使用Javascript进行XML解析显示.childNodes

学习XML & Javascript 但是在我的文件XML中显示And的.childNodes和.nodeValue时遇到问题。

  • 在w3schools.com和Google搜索失败:(
  • 我不明白为什么不显示数据......

欢迎任何帮助

先谢谢支持

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>

Code Javascript

<script>
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for 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 my test Studing

先谢谢支持

1 个答案:

答案 0 :(得分:0)

在IE浏览器中我得到:无法获得财产&#39; getElementsByTagName&#39;未定义或空引用。 &#34; X&#34; displayXMLeague(i)内部未定义,因此我将其更改为&#34; var x&#34;。我还假设您正在调用&#34; displayXMLeague&#34;功能。在下面的示例中,我只是将调用添加到底部:&#34; displayXMLeague(0);&#34;

        <div id="showXMLeague" />
        <script type="text/javascript">
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for 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>

你的输出:

Team_Name:Burnley
描述:fundado en 1882. PremierLeague 2013/14
城市:隆德雷斯 体育场:伯恩利
球员:Daniel Johnson
查尔斯N&#Z; Zogbia
image:images / xmleague / burnley.png