简体   繁体   English

Javascript无法生成包含XML信息的div。 (xml初学者)

[英]Javascript failure to generate divs that include XML info. (xml beginner)

I am trying to create a script that will generate divs depending on the information of the XML file that I desire. 我正在尝试创建一个脚本,该脚本将根据所需的XML文件的信息生成div。 The issue is that my script does not work at all. 问题是我的脚本根本不起作用。 What I did was to find the code on w3schools.org and modified it a bit to match what I wanted it to do. 我要做的是在w3schools.org上找到代码,并对其进行了一些修改以匹配我想要的代码。

The results: 结果:

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

var size=0;
var i=0;
var x=xmlDoc.getElementsByTagName("honey");
var y=x.length;
var final=null;

function gethoney()
{
   for (i < y) 
   { 
     a1='<div style="height:200px;width:300px;float:left;background-color:#FFF5CC;opacity:0.7;text-align:center;">';
     a2='<p><b>';
     a3=(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
     a4='<br>';
     a5=(x[i].getElementsByTagName("price")[0].childNodes[0].nodeValue);
     a6='</br></b></p>';
     a8='</div>';
     a9='<div style="height:200px;width:20px;float:left;"></div>';
     a10='<div style="height:200px;width:100px;float:left;">';
     a11=(x[i].getElementsByTagName("image")[0].childNodes[0].nodeValue);
     a12='</div>';
     final=final + a1 + a2 + a3 + a4 + a5 + a6 + a8 + a9 + a10 + a11 + a12;
     size=size+300;
     i=i+1;
   }
document.getElementById("mainbody").style.height=size;
size=size-20;
document.getElementById("products").style.height=size;
document.getElementById("products").innerHTML=final;
}
</script>

Using final in order to save every single div doesn't seem like an ideal way to me but I did it anyway. 对我来说,使用final来保存每个div似乎不是一种理想的方法,但无论如何我还是这么做了。

and the XML is this: XML是这样的:

<?xml version="1.0" encoding="windows-1252"?>
<honey>
   <name>one</name>
   <price>3</price>
   <image><img src=""></img></image>
   <disc>This is a honey</disc>
</honey>
<honey>
   <name>one</name>
   <price>3</price>
   <image><img src=""></img></image>
   <disc>This is a honey</disc>
</honey>
etc etc

I tried to find info about this but without any luck. 我试图找到有关此信息,但没有任何运气。 Could anyone point out what I am doing wrong and suggest improvements/corrections? 谁能指出我做错了什么,并提出改进/更正的建议? Also, it would be appreciated if someone could write down a way to generate the divs without using final to save them and then innerHTML. 同样,如果有人可以写下一种生成div的方法,而无需使用final来保存它们,然后保存innerHTML,将不胜感激。

1) Your xml file is not xml. 1)您的xml文件不是xml。

2) Change this: 2)更改此:

xmlDoc=xmlhttp.responseXML; 

to this: 对此:

xmlDoc = xmlhttp.responseXML.documentElement; 

2) 2)

for (i < y) 

That's an error. 那是一个错误。

4) You define a function called gethoney() that you never call. 4)您定义了一个从未调用的函数gethoney()。 But you shouldn't call that function until you can get something like this to work: 但是,除非可以使类似的功能起作用,否则您不应该调用该函数:

console.log("==>" + xmlDoc[0].getElementsByTagName("name")[0].childNodes[0].nodeValue);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM