我要在HTML中用JavaScript代码异步获取XML文件中一个节点的值比如 <?xml version="1.0" encoding="GB2312"?> 
<configuration> 
  <appSettings> 
      <add key="123" value="456"/> 
  </appSettings> 
</configuration> 我要在页面中异步获取 <add>节点中key属性和value属性的值!也就是得到123和456希望高手帮忙解决下,麻烦给下注释!谢谢!!!

解决方案 »

  1.   

    异步就是ajax了!在google中搜索一下有很多ajax responseXML的例子!
      

  2.   


    <html> 
        <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8" />
            <title>select娴嬭瘯 </title>
            <script>            function NewInfoAction()
                {
                    var xmlDom;  //XML DOM object
                    var xmlFile="tt.xml"; //xml file name
                    var ieFlag="ie";  //default
                    var isChrome = navigator.userAgent.indexOf('Chrome') != -1;
                    if(isChrome||!window.event) ieFlag="other"
     
                    //----------------------------start: create XML Dom-----------------------------------------
                    loadXML = function(fileRoute){
                        xmlDoc=null;
                        if (window.ActiveXObject){
                            xmlDoc = new ActiveXObject('Msxml2.DOMDocument');
                            xmlDoc.async=false;
                            xmlDoc.load(fileRoute);
                        }
                        else if (document.implementation && document.implementation.createDocument){
                            var xmlhttp = new window.XMLHttpRequest();
                            xmlhttp.open("GET",fileRoute,false);
                            xmlhttp.send(null);
                            var xmlDoc = xmlhttp.responseXML;
                        }
                        else {xmlDoc=null;}
                        return xmlDoc;
                    }
                    xmlDom=loadXML(xmlFile);                if(ieFlag=="ie"){ //MS IE
                        var key=(xmlDom.documentElement.getElementsByTagName("add")[0].getAttributeNode("key").value)//得到key属性
                        var value=(xmlDom.documentElement.getElementsByTagName("add")[0].getAttributeNode("value").value)//得到value属性
                    }
                }
                window.onload=function(){
                    NewInfoAction()
                }
            </script>
      

  3.   

    示例:
    http://www.w3schools.com/Ajax/ajax_responsexml.asp