使用javascript解析XML文件时出现问题(限制结果)
我在底部列出了从XML文件解析信息并正确运行的脚本。如果我想显示一个最大限制结果(例如5),我不能这样做。我认为“切片”功能是我所需要的。在简单的文本数组(“芒果”、“香蕉”、“猕猴桃”、“桃子”)中使用相同的函数没有问题。有什么建议吗?提前谢谢使用javascript解析XML文件时出现问题(限制结果),javascript,xml,parsing,limit,Javascript,Xml,Parsing,Limit,我在底部列出了从XML文件解析信息并正确运行的脚本。如果我想显示一个最大限制结果(例如5),我不能这样做。我认为“切片”功能是我所需要的。在简单的文本数组(“芒果”、“香蕉”、“猕猴桃”、“桃子”)中使用相同的函数没有问题。有什么建议吗?提前谢谢 if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code f
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 displayCDInfo(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);
country=(x[i].getElementsByTagName("COUNTRY")[0].childNodes[0].nodeValue);
company=(x[i].getElementsByTagName("COMPANY")[0].childNodes[0].nodeValue);
price=(x[i].getElementsByTagName("PRICE")[0].childNodes[0].nodeValue);
txt="Artist: "+artist+"<br />Title: "+title+"<br />Year: "+year+"<br />Country: "+country+"<br />Company: "+company+"<br />Price: "+price ;
document.getElementById("showCD").innerHTML=txt;
}
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
open(“GET”,“cd_catalog.xml”,false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
x=xmlDoc.getElementsByTagName(“CD”);
功能显示CDINFO(一)
{
Artister=(x[i].getElementsByTagName(“Artister”)[0].childNodes[0].nodeValue);
title=(x[i].getElementsByTagName(“title”)[0].childNodes[0].nodeValue);
年份=(x[i].getElementsByTagName(“年份”)[0].childNodes[0].nodeValue);
国家=(x[i].getElementsByTagName(“国家”)[0].childNodes[0].nodeValue);
company=(x[i].getElementsByTagName(“公司”)[0].childNodes[0].nodeValue);
价格=(x[i].getElementsByTagName(“价格”)[0].childNodes[0].nodeValue);
txt=“艺人:”+艺人+”
头衔:“+Title+”
年份:“+Year+”
国家:“+Country+”
公司:“+Company+”
价格:“+Price;
document.getElementById(“showCD”).innerHTML=txt;
}
不清楚您真正想要的是什么,但我认为您正在某处调用此函数,并且您的var x是一个数组。试试这个
for(var i=0;i<5;i++){
displayCDInfo(i)
}
for(var i=0;i不清楚您真正想要什么,但我认为您正在某处调用此函数,并且您的var x是一个数组。试试这个
for(var i=0;i<5;i++){
displayCDInfo(i)
}
for(var i=0;i您的回复是我所需要的。谢谢Rupesh!@user1685609如果需要,请单击我的答案右键签名接受答案:)您的回复是我所需要的。谢谢Rupesh!@user1685609如果需要,请单击我的答案右键签名接受答案:)