本帖最后由 geelpheels 于 2010-08-21 18:12:44 编辑

解决方案 »

  1.   

    <script>
    function requestHttp(url, type, data, loaded, error) {
    if (typeof loaded != "function") return;
    var xmlhttp = typeof XMLHttpRequest == "undefined" ?
    new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4)
    if (xmlhttp.status == 200)
    loaded(xmlhttp);
    else if (parseFloat(xmlhttp.status) > 300 && typeof error == "function")
    error(xmlhttp);
    }
    xmlhttp.open(typeof type == "string" ? type : "GET", url, true);
    if (typeof data == "string") {
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
    xmlhttp.setRequestHeader("Content-Length", data.length);
    }
    xmlhttp.send(data);
    }requestHttp("data.xml", "GET", null, function(xmlhttp) {
    if (!xmlhttp.responseXML) return;
    var xmldoc = xmlhttp.responseXML.documentElement;
    if (xmldoc.tagName != "all") return;
    var datas = [];
    var msg = []; // 测试输出用
    for (var i = 0; i < xmldoc.childNodes.length; i++) {
    var item = xmldoc.childNodes[i];
    if (item.tagName != "item") continue;
    var data = {};
    for (var j = 0; j < item.childNodes.length; j++) {
    switch (item.childNodes[j].tagName) {
    case "id":
    var node = item.childNodes[j];
    if (node.childNodes.length == 1 && node.childNodes[0].nodeType == 3)
    data.id = node.childNodes[0].nodeValue;
    break;
    case "name":
    var node = item.childNodes[j];
    if (node.childNodes.length == 1 && node.childNodes[0].nodeType == 3)
    data.name = node.childNodes[0].nodeValue;
    break;
    }
    }
    datas.push(data);
    msg.push([data.id, data.name].join("="));
    }
    alert(msg.join("\n"));
    });
    </script>
      

  2.   

    <html>
    <head>
    <script type="text/javascript">
    function load(){
      var xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
      xmlDoc.async = false;
      xmlDoc.load("a.xml");
      var doc = xmlDoc.documentElement;
      var a = doc.childNodes[0].childNodes[0];
      alert(a.getAttribute("value"));
    }
    </script>
    </head>
    <body onload="load()">
    </body>
    </html>添加节点
    XmlDocument xmlDoc=new XmlDocument();  
    xmlDoc.Load("");  
    XmlNode root=xmlDoc.SelectSingleNode("sList");
    XmlElement xe1=xmlDoc.CreateElement("s");  XmlElement xesub1=xmlDoc.CreateElement("key");  
    xesub1.InnerText="";  
    xe1.AppendChild(xesub1);
    root.AppendChild(xe1);
    xmlDoc.Save("");