var xmlhttp =  new XMLHttpRequest();
xmlhttp.open('POST', '../servlet/ajaxServlet', true);
    
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200){
            alert(xmlhttp.responseXML.xml);
            alert(new XMLSerializer().serializeToString(xmlhttp.responseXML.getElementsByTagName("div")[0]));           
        }
    }
}
我修改服务器端送回来的xml字段(xmlhttp.responseXML.xml),
如果是 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><html xmlns=\"http://www.w3.org/1999/xhtml\" version=\"-//W3C//DTD HTML 4.01 Transitional//EN\"><body><div>test</div></body></html>";
就取不到值。如果是 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><html><body><div>test</div></body></html>",就能alert出来。和解?       
怎么样才能把第一种情况的div值取出来?

解决方案 »

  1.   

    xmlhttp.responseXML.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "div")[0]
      

  2.   

    怎么样加名称空间呢?
    我用xmlhttp.responseXML.getElementsByTagNameNS("xmlns=\"http://www.w3.org/1999/xhtml\"","div")[0]
    结果提示没有这个属性或者方法。
      

  3.   

    为了处理简单,不建议返回
     xmlns=\"http://www.w3.org/1999/xhtml\" version=\"-//W3C//DTD HTML 4.01 Transitional//EN\"这些内容
      

  4.   

    In Firefox 2 (Gecko 1.8.1) and earlier this method didn't work correctly if the subtree had elements with namespace prefix in the tag name (See bug 206053 for details.) It's recommended to use element.getElementsByTagNameNS when dealing with multi-namespace documents. 
      

  5.   

    我已经这样写了,而且我的浏览器是IE6,
    xmlhttp.responseXML.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "div")[0]
    结果返回叫本错误说不支持这个属性或者方法之类的。
      

  6.   

    xmlhttp.responseXML没有getElementsByTagNameNS方法?
    怎么回事?奇怪。
      

  7.   

    getElementsByTagNameNS是DOM的方法,不是xmldom方法
      

  8.   

    那,咋写捏~
    用字符串把xmlhttp.responseXML.xml读出来,然后
    在用一个dom把它load回去?好像有点怪.....还是,有什么好的方法解决这个问题吗?
    写个例子看看啊老大。
      

  9.   

    服务器返回来的数据无法改动,然后能把div这个tag读出来,就可以了。嘿嘿.....
      

  10.   

    <script>str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><html xmlns=\"http://www.w3.org/1999/xhtml\" version=\"-//W3C//DTD HTML 4.01 Transitional//EN\"><body><div>test</div></body></html>"
    var doc = new ActiveXObject("MSxml2.DOMDocument")
    alert(doc.loadXML(str));
    doc.setProperty("SelectionNamespaces", "xmlns:x='http://www.w3.org/1999/xhtml'")
    var node = doc.selectSingleNode("//x:div")
    alert(node.text)
    </script>如果在你的代码可以直接var doc = xmlhttp.responseXMLdoc.setProperty("SelectionNamespaces", "xmlns:x='http://www.w3.org/1999/xhtml'")
    var node = doc.selectSingleNode("//x:div")
    alert(node.text)
      

  11.   


    var doc = xmlhttp.responseXMLdoc.setProperty("SelectionNamespaces", "xmlns:x='http://www.w3.org/1999/xhtml'")
    var node = doc.selectNodes("//x:div")[0]
    alert(node.text)
    也可以
      

  12.   

    啊啊啊啊啊~~
    老大你太厉害了!
    烦了我一天半的问题被你三两下搞定了。
    pfpf。结帐~