日期:2014-05-16  浏览次数:20520 次

问一个小问题,谢谢!
客户端有一个xml文件请问js是否有可以访问其中数据的功能

------解决方案--------------------
<script>
var xmlDoc= new ActiveXObject( 'Microsoft.XMLDOM ');
xmlDoc.async=false;
xmlDoc.loadXML(src);//src为xml文件的路径
alert(xmlDoc.xml);
</script>
------解决方案--------------------
HTML文件代码:
<html>
<head>
<title> Ajax Hello World </title>
<script type= "text/javascript ">
var xmlHttp;

function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP ");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}

function startRequest(){
createXMLHttpRequest();
try{
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open( "GET ", "data.xml ", true);
xmlHttp.send(null);
}catch(exception){
alert( "eeee! ");
}
}

function handleStateChange(){
if(xmlHttp.readyState == 4){
if (xmlHttp.status == 200 || xmlHttp.status == 0){

var xmlDOM = xmlHttp.responseXML;

var root = xmlDOM.documentElement;
try
{

var info = root.getElementsByTagName( 'info ');

alert( "responseXML 's value: " + info[0].firstChild.data);
}catch(exception)
{
alert( "The source is not exist! ");
}
}
}
}
</script>
</head>
<body>
<div>
<input type= "button " value= "return ajax responseXML 's value "
onclick= "startRequest(); " />
</div>
</body>
</html>

xml文件:
<?xml version= "1.0 " encoding= "GB2312 " ?>
<root>
<info> hello world! </info>
</root>
------解决方案--------------------
nodeValue
nodeName
xml dom的标准属性都可以访问
------解决方案--------------------
楼上的这些方法只有在IE6以下版本的浏览器中才可能。IE7和火狐是不强安全机制,不支持浏览本地盘符。