打开XML并在div中显示数据

时间:2011-08-10 00:28:52

标签: javascript xml

我是xml的新手,发现这个示例是我加载到计算机上的目录中。

XML文件

  <?xml version="1.0" encoding="ISO-8859-1" ?> 
- <!--  Edited by XMLSpy® 
  --> 
- <CATALOG>
- <CD>
  <TITLE>Empire Burlesque</TITLE> 
  <ARTIST>Bob Dylan</ARTIST> 
  <COUNTRY>USA</COUNTRY> 
  <COMPANY>Columbia</COMPANY> 
  <PRICE>10.90</PRICE> 
  <YEAR>1985</YEAR> 
  </CD>
- <CD>
  <TITLE>Hide your heart</TITLE> 
  <ARTIST>Bonnie Tyler</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>CBS Records</COMPANY> 
  <PRICE>9.90</PRICE> 
  <YEAR>1988</YEAR> 
  </CD>
- <CD>
  <TITLE>Greatest Hits</TITLE> 
  <ARTIST>Dolly Parton</ARTIST> 
  <COUNTRY>USA</COUNTRY> 
  <COMPANY>RCA</COMPANY> 
  <PRICE>9.90</PRICE> 
  <YEAR>1982</YEAR> 
  </CD>
- <CD>
  <TITLE>Still got the blues</TITLE> 
  <ARTIST>Gary Moore</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>Virgin records</COMPANY> 
  <PRICE>10.20</PRICE> 
  <YEAR>1990</YEAR> 
  </CD>
- <CD>
  <TITLE>Eros</TITLE> 
  <ARTIST>Eros Ramazzotti</ARTIST> 
  <COUNTRY>EU</COUNTRY> 
  <COMPANY>BMG</COMPANY> 
  <PRICE>9.90</PRICE> 
  <YEAR>1997</YEAR> 
  </CD>
- <CD>
  <TITLE>One night only</TITLE> 
  <ARTIST>Bee Gees</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>Polydor</COMPANY> 
  <PRICE>10.90</PRICE> 
  <YEAR>1998</YEAR> 
  </CD>
- <CD>
  <TITLE>Sylvias Mother</TITLE> 
  <ARTIST>Dr.Hook</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>CBS</COMPANY> 
  <PRICE>8.10</PRICE> 
  <YEAR>1973</YEAR> 
  </CD>
- <CD>
  <TITLE>Maggie May</TITLE> 
  <ARTIST>Rod Stewart</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>Pickwick</COMPANY> 
  <PRICE>8.50</PRICE> 
  <YEAR>1990</YEAR> 
  </CD>
- <CD>
  <TITLE>Romanza</TITLE> 
  <ARTIST>Andrea Bocelli</ARTIST> 
  <COUNTRY>EU</COUNTRY> 
  <COMPANY>Polydor</COMPANY> 
  <PRICE>10.80</PRICE> 
  <YEAR>1996</YEAR> 
  </CD>
- <CD>
  <TITLE>When a man loves a woman</TITLE> 
  <ARTIST>Percy Sledge</ARTIST> 
  <COUNTRY>USA</COUNTRY> 
  <COMPANY>Atlantic</COMPANY> 
  <PRICE>8.70</PRICE> 
  <YEAR>1987</YEAR> 
  </CD>
- <CD>
  <TITLE>Black angel</TITLE> 
  <ARTIST>Savage Rose</ARTIST> 
  <COUNTRY>EU</COUNTRY> 
  <COMPANY>Mega</COMPANY> 
  <PRICE>10.90</PRICE> 
  <YEAR>1995</YEAR> 
  </CD>
- <CD>
  <TITLE>1999 Grammy Nominees</TITLE> 
  <ARTIST>Many</ARTIST> 
  <COUNTRY>USA</COUNTRY> 
  <COMPANY>Grammy</COMPANY> 
  <PRICE>10.20</PRICE> 
  <YEAR>1999</YEAR> 
  </CD>
- <CD>
  <TITLE>For the good times</TITLE> 
  <ARTIST>Kenny Rogers</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>Mucik Master</COMPANY> 
  <PRICE>8.70</PRICE> 
  <YEAR>1995</YEAR> 
  </CD>
- <CD>
  <TITLE>Big Willie style</TITLE> 
  <ARTIST>Will Smith</ARTIST> 
  <COUNTRY>USA</COUNTRY> 
  <COMPANY>Columbia</COMPANY> 
  <PRICE>9.90</PRICE> 
  <YEAR>1997</YEAR> 
  </CD>
- <CD>
  <TITLE>Tupelo Honey</TITLE> 
  <ARTIST>Van Morrison</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>Polydor</COMPANY> 
  <PRICE>8.20</PRICE> 
  <YEAR>1971</YEAR> 
  </CD>
- <CD>
  <TITLE>Soulsville</TITLE> 
  <ARTIST>Jorn Hoel</ARTIST> 
  <COUNTRY>Norway</COUNTRY> 
  <COMPANY>WEA</COMPANY> 
  <PRICE>7.90</PRICE> 
  <YEAR>1996</YEAR> 
  </CD>
- <CD>
  <TITLE>The very best of</TITLE> 
  <ARTIST>Cat Stevens</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>Island</COMPANY> 
  <PRICE>8.90</PRICE> 
  <YEAR>1990</YEAR> 
  </CD>
- <CD>
  <TITLE>Stop</TITLE> 
  <ARTIST>Sam Brown</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>A and M</COMPANY> 
  <PRICE>8.90</PRICE> 
  <YEAR>1988</YEAR> 
  </CD>
- <CD>
  <TITLE>Bridge of Spies</TITLE> 
  <ARTIST>T'Pau</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>Siren</COMPANY> 
  <PRICE>7.90</PRICE> 
  <YEAR>1987</YEAR> 
  </CD>
- <CD>
  <TITLE>Private Dancer</TITLE> 
  <ARTIST>Tina Turner</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>Capitol</COMPANY> 
  <PRICE>8.90</PRICE> 
  <YEAR>1983</YEAR> 
  </CD>
- <CD>
  <TITLE>Midt om natten</TITLE> 
  <ARTIST>Kim Larsen</ARTIST> 
  <COUNTRY>EU</COUNTRY> 
  <COMPANY>Medley</COMPANY> 
  <PRICE>7.80</PRICE> 
  <YEAR>1983</YEAR> 
  </CD>
- <CD>
  <TITLE>Pavarotti Gala Concert</TITLE> 
  <ARTIST>Luciano Pavarotti</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>DECCA</COMPANY> 
  <PRICE>9.90</PRICE> 
  <YEAR>1991</YEAR> 
  </CD>
- <CD>
  <TITLE>The dock of the bay</TITLE> 
  <ARTIST>Otis Redding</ARTIST> 
  <COUNTRY>USA</COUNTRY> 
  <COMPANY>Atlantic</COMPANY> 
  <PRICE>7.90</PRICE> 
  <YEAR>1987</YEAR> 
  </CD>
- <CD>
  <TITLE>Picture book</TITLE> 
  <ARTIST>Simply Red</ARTIST> 
  <COUNTRY>EU</COUNTRY> 
  <COMPANY>Elektra</COMPANY> 
  <PRICE>7.20</PRICE> 
  <YEAR>1985</YEAR> 
  </CD>
- <CD>
  <TITLE>Red</TITLE> 
  <ARTIST>The Communards</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>London</COMPANY> 
  <PRICE>7.80</PRICE> 
  <YEAR>1987</YEAR> 
  </CD>
- <CD>
  <TITLE>Unchain my heart</TITLE> 
  <ARTIST>Joe Cocker</ARTIST> 
  <COUNTRY>USA</COUNTRY> 
  <COMPANY>EMI</COMPANY> 
  <PRICE>8.20</PRICE> 
  <YEAR>1987</YEAR> 
  </CD>
  </CATALOG>

HTML文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>

<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","cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

x=xmlDoc.getElementsByTagName("CD");

function displayCD(i)
{
artist=(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
title=(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
year=(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue);
txt="Artist: " + artist + "<br />Title: " + title + "<br />Year: "+ year;
document.getElementById("showCD").innerHTML=txt;
}
</script>
</head>
<body onload="displayCD(2)">

<div id='showCD'></div>

</body>
</html>

浏览器中出现错误,指出“x”未定义。此简单示例是否需要特定设置才能生效?

3 个答案:

答案 0 :(得分:1)

“(...)加载到我计算机上的目录中” 这是问题所在。由于安全原因,这是默认的。您应该在服务器上执行它(它可以是本地服务器)。

有些浏览器允许,但你必须更改配置(例如在Opera opera:config#UserPrefs|AllowFileXMLHttpRequest中)。

编辑。

我检查了一下。在Opera Allow File XMLHttpRequest中设置后,一切正常。 在Firefox 5.0中,它默认为我工作。

EDIT2。

我找到了解决方案! :)

您的代码应如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>

<script type="text/javascript">
xmlhttp = "";
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","http://"+location.host+"/cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

x=xmlDoc.getElementsByTagName("CD");

function displayCD(i)
{
artist=(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
title=(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
year=(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue);
txt="Artist: " + artist + "<br />Title: " + title + "<br />Year: "+ year;
document.getElementById("showCD").innerHTML=txt;
}
</script>
</head>
<body onload="displayCD(2)">

<div id='showCD'></div>

</body>
</html>
  1. 阅读我的原始答案。
  2. 阅读我的评论:我不知道为什么,但在这种情况下,您需要传递绝对路径(而不是相对路径)才能使此脚本在IE中运行。
  3. 您需要将此行xmlhttp = "";放在开头,以使其在IE中运行。
  4. 这就是全部。 :)

答案 1 :(得分:0)

编辑:忽略我的回答,我没注意到您将异步设置为false。

您的问题是您在实际收到数据之前尝试对数据执行某些操作。您必须使用onreadystatechange事件来捕获状态和数据。我只想引用MDN,因为他们在实际解释它时做得更好:

  

[...]您需要在收到服务器对您的请求的回复后决定您要做什么。在这个阶段,您只需要告诉HTTP请求对象哪个JavaScript函数将处理响应。这是通过将对象的onreadystatechange属性设置为当请求的状态发生变化时应该调用的JavaScript函数的名称来完成的[...]

他们有一个方便的指南:https://developer.mozilla.org/en/AJAX/Getting_Started

答案 2 :(得分:0)

xml文件格式是问题所在。当我找到一个有效的不同演示时,我发现了。

此HTML有效。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>

<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","cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

x=xmlDoc.getElementsByTagName("CD");

function displayCD(i)
{
artist=(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
title=(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
year=(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue);
txt="Artist: " + artist + "<br />Title: " + title + "<br />Year: "+ year;
document.getElementById("showCD").innerHTML=txt;
}
</script>
</head>
<body onload="displayCD(5)">

<div id='showCD'></div>

</body>
</html>

使用这个xml。

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<CATALOG>
<CD>
  <TITLE>Empire Burlesque</TITLE> 
  <ARTIST>Bob Dylan</ARTIST> 
  <COUNTRY>USA</COUNTRY> 
  <COMPANY>Columbia</COMPANY> 
  <PRICE>10.90</PRICE> 
  <YEAR>1985</YEAR> 
  </CD>
<CD>
  <TITLE>Hide your heart</TITLE> 
  <ARTIST>Bonnie Tyler</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>CBS Records</COMPANY> 
  <PRICE>9.90</PRICE> 
  <YEAR>1988</YEAR> 
  </CD>
<CD>
  <TITLE>Greatest Hits</TITLE> 
  <ARTIST>Dolly Parton</ARTIST> 
  <COUNTRY>USA</COUNTRY> 
  <COMPANY>RCA</COMPANY> 
  <PRICE>9.90</PRICE> 
  <YEAR>1982</YEAR> 
  </CD>
<CD>
  <TITLE>Still got the blues</TITLE> 
  <ARTIST>Gary Moore</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>Virgin records</COMPANY> 
  <PRICE>10.20</PRICE> 
  <YEAR>1990</YEAR> 
  </CD>
<CD>
  <TITLE>Eros</TITLE> 
  <ARTIST>Eros Ramazzotti</ARTIST> 
  <COUNTRY>EU</COUNTRY> 
  <COMPANY>BMG</COMPANY> 
  <PRICE>9.90</PRICE> 
  <YEAR>1997</YEAR> 
  </CD>
<CD>
  <TITLE>One night only</TITLE> 
  <ARTIST>Bee Gees</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>Polydor</COMPANY> 
  <PRICE>10.90</PRICE> 
  <YEAR>1998</YEAR> 
  </CD>
<CD>
  <TITLE>Sylvias Mother</TITLE> 
  <ARTIST>Dr.Hook</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>CBS</COMPANY> 
  <PRICE>8.10</PRICE> 
  <YEAR>1973</YEAR> 
  </CD>
<CD>
  <TITLE>Maggie May</TITLE> 
  <ARTIST>Rod Stewart</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>Pickwick</COMPANY> 
  <PRICE>8.50</PRICE> 
  <YEAR>1990</YEAR> 
  </CD>
<CD>
  <TITLE>Romanza</TITLE> 
  <ARTIST>Andrea Bocelli</ARTIST> 
  <COUNTRY>EU</COUNTRY> 
  <COMPANY>Polydor</COMPANY> 
  <PRICE>10.80</PRICE> 
  <YEAR>1996</YEAR> 
  </CD>
<CD>
  <TITLE>When a man loves a woman</TITLE> 
  <ARTIST>Percy Sledge</ARTIST> 
  <COUNTRY>USA</COUNTRY> 
  <COMPANY>Atlantic</COMPANY> 
  <PRICE>8.70</PRICE> 
  <YEAR>1987</YEAR> 
  </CD>
<CD>
  <TITLE>Black angel</TITLE> 
  <ARTIST>Savage Rose</ARTIST> 
  <COUNTRY>EU</COUNTRY> 
  <COMPANY>Mega</COMPANY> 
  <PRICE>10.90</PRICE> 
  <YEAR>1995</YEAR> 
  </CD>
<CD>
  <TITLE>1999 Grammy Nominees</TITLE> 
  <ARTIST>Many</ARTIST> 
  <COUNTRY>USA</COUNTRY> 
  <COMPANY>Grammy</COMPANY> 
  <PRICE>10.20</PRICE> 
  <YEAR>1999</YEAR> 
  </CD>
<CD>
  <TITLE>For the good times</TITLE> 
  <ARTIST>Kenny Rogers</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>Mucik Master</COMPANY> 
  <PRICE>8.70</PRICE> 
  <YEAR>1995</YEAR> 
  </CD>
<CD>
  <TITLE>Big Willie style</TITLE> 
  <ARTIST>Will Smith</ARTIST> 
  <COUNTRY>USA</COUNTRY> 
  <COMPANY>Columbia</COMPANY> 
  <PRICE>9.90</PRICE> 
  <YEAR>1997</YEAR> 
  </CD>
<CD>
  <TITLE>Tupelo Honey</TITLE> 
  <ARTIST>Van Morrison</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>Polydor</COMPANY> 
  <PRICE>8.20</PRICE> 
  <YEAR>1971</YEAR> 
  </CD>
<CD>
  <TITLE>Soulsville</TITLE> 
  <ARTIST>Jorn Hoel</ARTIST> 
  <COUNTRY>Norway</COUNTRY> 
  <COMPANY>WEA</COMPANY> 
  <PRICE>7.90</PRICE> 
  <YEAR>1996</YEAR> 
  </CD>
<CD>
  <TITLE>The very best of</TITLE> 
  <ARTIST>Cat Stevens</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>Island</COMPANY> 
  <PRICE>8.90</PRICE> 
  <YEAR>1990</YEAR> 
  </CD>
<CD>
  <TITLE>Stop</TITLE> 
  <ARTIST>Sam Brown</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>A and M</COMPANY> 
  <PRICE>8.90</PRICE> 
  <YEAR>1988</YEAR> 
  </CD>
<CD>
  <TITLE>Bridge of Spies</TITLE> 
  <ARTIST>T'Pau</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>Siren</COMPANY> 
  <PRICE>7.90</PRICE> 
  <YEAR>1987</YEAR> 
  </CD>
<CD>
  <TITLE>Private Dancer</TITLE> 
  <ARTIST>Tina Turner</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>Capitol</COMPANY> 
  <PRICE>8.90</PRICE> 
  <YEAR>1983</YEAR> 
  </CD>
<CD>
  <TITLE>Midt om natten</TITLE> 
  <ARTIST>Kim Larsen</ARTIST> 
  <COUNTRY>EU</COUNTRY> 
  <COMPANY>Medley</COMPANY> 
  <PRICE>7.80</PRICE> 
  <YEAR>1983</YEAR> 
  </CD>
<CD>
  <TITLE>Pavarotti Gala Concert</TITLE> 
  <ARTIST>Luciano Pavarotti</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>DECCA</COMPANY> 
  <PRICE>9.90</PRICE> 
  <YEAR>1991</YEAR> 
  </CD>
<CD>
  <TITLE>The dock of the bay</TITLE> 
  <ARTIST>Otis Redding</ARTIST> 
  <COUNTRY>USA</COUNTRY> 
  <COMPANY>Atlantic</COMPANY> 
  <PRICE>7.90</PRICE> 
  <YEAR>1987</YEAR> 
  </CD>
<CD>
  <TITLE>Picture book</TITLE> 
  <ARTIST>Simply Red</ARTIST> 
  <COUNTRY>EU</COUNTRY> 
  <COMPANY>Elektra</COMPANY> 
  <PRICE>7.20</PRICE> 
  <YEAR>1985</YEAR> 
  </CD>
<CD>
  <TITLE>Red</TITLE> 
  <ARTIST>The Communards</ARTIST> 
  <COUNTRY>UK</COUNTRY> 
  <COMPANY>London</COMPANY> 
  <PRICE>7.80</PRICE> 
  <YEAR>1987</YEAR> 
  </CD>
<CD>
  <TITLE>Unchain my heart</TITLE> 
  <ARTIST>Joe Cocker</ARTIST> 
  <COUNTRY>USA</COUNTRY> 
  <COMPANY>EMI</COMPANY> 
  <PRICE>8.20</PRICE> 
  <YEAR>1987</YEAR> 
  </CD>
  </CATALOG>

再次感谢您在此工作。