下面的XML解析是一位大哥写给我的,但在我本机IE浏览器运行没效果,望各位大虾看看是什么问题
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <script type="text/javascript">
            function creatXHR(){
                if (window.ActiveXObject) return new window.ActiveXObject('Microsoft.XMLDOM');
                else if (window.XMLHttpRequest) return new window.XMLHttpRequest();
                else return null;
            }
            
            function getText(node){
                return node.xml ? node.xml : (new XMLSerializer()).serializeToString(node);
            }
            
            function done(xhr){
                var text = getText(xhr.responseXML.getElementsByTagName('div')[0]);
                document.getElementById("aa").innerHTML = text;
            }
        </script>
    </head>
    <body>
        <div id="aa" onClick="a();" style="width:400px; height:400px; background-color:#0066FF;"></div>
        <script type="text/javascript">
function a(){
            var xhr = creatXHR();
            xhr.open('GET', 'image-flow.xml?r='+ (new Date()).getTime(), false);
            xhr.send(null);
            try {
                done(xhr);
            } catch (e) {
                xhr.onreadystatechange = function(){
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        done(xhr);
                    }
                }
            }
}
        </script>
    </body>
</html>
<?xml version="1.0" encoding="utf-8"?> 
<div>
<img src="boom.jpg"></img>
</div>

解决方案 »

  1.   


    <html> 
    <head> 
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <script type="text/javascript">
                function creatXHR(){
                    if (document.all){
                     try {return new ActiveXObject('Msxml2.XMLHTTP');}
                    catch(e){return new ActiveXObject('Microsoft.XMLHTTP');};
                }
                    else if (window.XMLHttpRequest) return new window.XMLHttpRequest();
                    else return null;
                }
                function getText(node){
                    return node.xml ? node.xml : (new XMLSerializer()).serializeToString(node);
                }
                
                function done(xhr){
                    var text = getText(xhr.responseXML.getElementsByTagName('div')[0]);
                    document.getElementById("aa").innerHTML = text;
                }
            </script>
        </head>
        <body>
            <div id="aa" onClick="a();" style="width:400px; height:400px; background-color:#0066FF;"></div>
            <script type="text/javascript">
            function a(){
                var xhr = creatXHR();
                xhr.open('GET', 'info.xml?r='+ escape(new Date()), false);
                xhr.send(null);
                try {
                    done(xhr);
                } catch (e) {
                    xhr.onreadystatechange = function(){
                        if (xhr.readyState == 4 && xhr.status == 200) {
                            done(xhr);
                        }
                    }
                }
                }
            </script>
        </body>
    </html>
      

  2.   

    上面xml文件名字改一下<html> 
    <head> 
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <script type="text/javascript">
                function creatXHR(){
                    if (document.all){
                     try {return new ActiveXObject('Msxml2.XMLHTTP');}
                    catch(e){return new ActiveXObject('Microsoft.XMLHTTP');};
                }
                    else if (window.XMLHttpRequest) return new window.XMLHttpRequest();
                    else return null;
                }
                function getText(node){
                    return node.xml ? node.xml : (new XMLSerializer()).serializeToString(node);
                }
                
                function done(xhr){
                    var text = getText(xhr.responseXML.getElementsByTagName('div')[0]);
                    document.getElementById("aa").innerHTML = text;
                }
            </script>
        </head>
        <body>
            <div id="aa" onClick="a();" style="width:400px; height:400px; background-color:#0066FF;"></div>
            <script type="text/javascript">
            function a(){
                var xhr = creatXHR();
                xhr.open('GET', 'image-flow.xml?r='+ escape(new Date()), false);
                xhr.send(null);
                try {
                    done(xhr);
                } catch (e) {
                    xhr.onreadystatechange = function(){
                        if (xhr.readyState == 4 && xhr.status == 200) {
                            done(xhr);
                        }
                    }
                }
                }
            </script>
        </body>
    </html>
      

  3.   

    LZ你在IE上到底报什么错 我这里测试都是好的
      

  4.   

    如果没有去掉try catch 语句就不会报错,但是还是没效果
    如果去掉try catch 判断,直接done(xhr),就会报以下行缺少对象的错误。
    return node.xml ? node.xml : (new XMLSerializer()).serializeToString(node);
      

  5.   

    你直接看看xhr.responseXML.getElementsByTagName('div')[0]有值没
      

  6.   

    是使用http://localhost/xx.html方式访问的?
      

  7.   

    XMLSerializer 这个东西只适用于Microsoft Visual Studio .NET吧?!如果仅仅IE的话

      function done(xhr){
                    var text = getText(xhr.responseXML.getElementsByTagName('div')[0]);
                    document.getElementById("aa").innerHTML = text;
               }改成 function creatXHR(){
             if (window.ActiveXObject) return new window.ActiveXObject('Microsoft.XMLHTTP');
            else if (window.XMLHttpRequest) return new window.XMLHttpRequest();
            else return null;
      }  function done(xhr){
                    var text = xhr.responseXML.getElementsByTagName("div")[0].childNodes[0].xml;
                    document.getElementById("aa").innerHTML = text;
                }
      

  8.   


    没,调用getText()函数,执行以下语句就断了
    return node.xml ? node.xml : (new XMLSerializer()).serializeToString(node);所以alert(text)没有效果
      

  9.   


    不是,是C:\Documents and Settings\Administrator\桌面\7657252.html
      

  10.   

    LZ你运行本地的页面是无法使用XHR的,当然无效咯
      

  11.   


    FF,SAFARI都是file:///C:/Documents%20and%20Settings/Administrator/%E6%A1%8C%E9%9D%A2/7657252.html方式请问怎么调运行方式?调成这个或者http://localhost/xx.html这种方式
      

  12.   

    XHR.send()后没有接收的的服务器怎么会有返回的数据呢
    当然要建立服务器