zoukankan      html  css  js  c++  java
  • 用 AJAX 读取xml 节点属性值

    <html>
    <head>
    <title>AjaxTest</title>
    <script>
    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("xmlHttp Fail");
        }
    }
    function handleStateChange()
    {    
        if(xmlHttp.readyState == 4)
        {        
            if (xmlHttp.status == 200 || xmlHttp.status == 0)
            {
                var root = xmlHttp.responseXML.documentElement;            
                try
                {
                    var info = root.getElementsByTagName("info")[0];
                    alert(info.getAttribute('type'));
                }
                catch(exception)
                {
                    alert("The node is not exist");
                }
            }
        }
    }
    </script>
    </head>
    <body>
        <div>
            <input type="button" value="AjaxTest" onclick="startRequest();" />
        </div>
    </body>
    </html>
     
    
    
    XML code <?xml version="1.0" encoding="GB2312"?>
    <root>
        <info type="student"></info>
    </root>
    

      

  • 相关阅读:
    RQNOJ 34 紧急援救
    Codevs 2080 特殊的质数肋骨
    POJ2975 Nim
    Bzoj1016 最小生成树计数
    POJ3613 Cow Relays
    POJ1386 Play on Words
    [从hzwer神犇那翻到的模拟赛题] 合唱队形
    HDU2824 The Euler function
    HDU1576 A/B
    HDU2669 Romantic
  • 原文地址:https://www.cnblogs.com/jameslif/p/3677951.html
Copyright © 2011-2022 走看看