xmlhttp问题:用JS如何获取XML内指定的信息??急.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<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 ", "gadget.xml ",false);
request.send()
var bob=request.responseText;
alert( "返回的信息为: "+bob)
}
//-->
</script>
</head>
<body>
<input id= "name " name= "name " type= "button " Value= "测试 " onclick= "return read(); ">
</body>
</html>
//这是第一个文件
<?xml version= "1.0 " encoding= "utf-8 " ?>
<gadget>
<name> mini Player! </name>
<namespace> miniPlayer </namespace>
<version> 1.0 </version>
<author name= "Mictel ">
<logo src= "icon.png "/>
</author>
<copyright> 2007 </copyright>
<description> Vista 边栏工具 Windows mini Player! </description>
<icons>
<icon height= "128 " width= "128 " src= "icon.png "/>
</icons>
<hosts>
<host name= "sidebar ">
<base type= "HTML " apiVersion= "1.0.0 " src= "VistaMP.html " />
<permissions> full </permissions>
<platform minPlatformVersion= "1.0 " />
<defaultImage src= "icon.png "/>
</host>
</hosts>
</gadget>
//这是第二个文件
以上文件测试成功.得到的是XML文件的全部信息.我的想法是:不是得到全部的信息.我只要XML里的某一条信息..如:(Vista 边栏工具 Windows mini Player!).想求几位高手帮帮我..在第一个文件的基础上帮我完成这个功能.小弟感激不尽..不够可以再散分的..
[解决办法]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<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 ", "gadget.xml ",false);
request.send()
var bob=request.responseText;
alert( "返回的信息为: "+bob)
var doc = new ActiveXObject( "MSXML2.DOMDocument ")
doc.loadXML(bob);
var node = doc.selectSingleNode( "//description ");
alert(node.text);
node = null;
doc = null;
}
//-->
</script>
</head>
<body>
<input id= "name " name= "name " type= "button " Value= "测试 " onclick= "return read(); ">
</body>
</html>
[解决办法]
xmldom = new ActiveXObject( "MSXML.DOMDocument ");
xmldom.loadXML(request.responseText);
alert(xmldom.selectSingleNode( "/gadget/name ").text);
alert(xmldom.selectSingleNode( "/gadget/description ").text)