怎么把xml里面的地址取出来循环输出...title也一样
test.xml
<channel>
<item> <title> 不知道选什么 </title> <link> http://rss.xinhuanet.com/rss/it.xml </link> </item>
<item> <title> dfghjkl; </title> <link> http://rss.xinhuanet.com/rss/it.xml </link> </item>
</channel>
index.html
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> 无标题文档 </title>
<script>
<!--
function read()
{
var request;
try {
request = new XMLHttpRequest();
}
catch(e){
request = new ActiveXObject( "Microsoft.XMLHTTP ");
}
request.open( "get ", "test.xml ",false);
request.send()
var bob=request.responseText;
alert( "返回的信息为: "+bob)
var doc = new ActiveXObject( "MSXML2.DOMDocument ")
doc.loadXML(bob);
var a = doc.getElementsByTagName( "item ")[0];
var b = doc.getElementsByTagName( "link ")[0];
alert(b);
}
//-->
</script>
</head>
<body>
<input id= "name " name= "name " type= "button " Value= "测试 " onClick= "return read(); ">
</body>
</html>
怎么把xml里面的地址取出来循环输出...title也一样.谢谢各位了..
------解决方案--------------------var links = doc.getElementsByTagName( "link ");
for(var i=0; i < links.length; i++) alert(links[i]);
O.o
------解决方案--------------------var items= doc.getElementsByTagName( "item ");
for(var i=0; i < items.length; i++) {
var childs=items[i].childNodes;
if(childs!=null){
for(var j=0;j <childs.length;j++){
alert(childs[j].firstChild.nodeValue);
}
}
}
------解决方案-------------------- <head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> 无标题文档 </title>
<script>
<!--
function read()
{
var str= ' <channel> <item> <title> 不知道选什么 </title> <link> http://rss.xinhuanet.com/rss/it.xml </link> </item> <item> <title> dfghjkl; </title> <link> http://rss.xinhuanet.com/rss/it.xml </link> </item> </channel> ';
var doc = new ActiveXObject( "MSXML2.DOMDocument ")
doc.loadXML(str);
var items= doc.getElementsByTagName( "item ");
for(var i=0; i < items.length; i++) {
var childs=items[i].childNodes;
if(childs!=null){
for(var j=0;j <childs.length;j++){
alert(childs[j].firstChild.nodeValue);
}
}
}
}
//-->
</script>
</head>
<body>
<input id= "name " name= "name " type= "button " Value= "测试 " onClick= "return read(); ">
</body>
</html>
把str的定义那部分替换成你获取xml的代码那部分就可以了
------解决方案-------------------- <head>
<meta http-equiv= "Content-Type " content= "text/html;