• js解析xml,不跨域


    从w3c看到的例子,自己测试了一下。ok。

    js解析xml,暂时没有找到可以跨域的办法。跨域的话,只能通过后台去写。

    javascript

    <script type="text/javascript">
            var xmlhttp;
    
            function loadXMLDoc(url) {
                xmlhttp = null;
                if (window.XMLHttpRequest) {// code for IE7, Firefox, Mozilla, etc.
                    xmlhttp = new XMLHttpRequest();
                }
                else if (window.ActiveXObject) {// code for IE5, IE6
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                if (xmlhttp != null) {
                    xmlhttp.onreadystatechange = onResponse;
                    xmlhttp.open("GET", url, true);
                    xmlhttp.send(null);
                }
                else {
                    alert("Your browser does not support XMLHTTP.");
                }
            }
    
            function onResponse() {
                if (xmlhttp.readyState != 4) return;
                if (xmlhttp.status != 200) {
                    alert("Problem retrieving XML data");
                    return;
                }
    
                txt = "<table border='1'>";
                x = xmlhttp.responseXML.documentElement.getElementsByTagName("item");
    
                for (i = 0; i < x.length; i++) {
                    txt = txt + "<tr>";
                    xx1 = x[i].getElementsByTagName("title");
                    {
                        try {
                            txt = txt + "<td>" + xx1[0].firstChild.nodeValue + "</td>";
                        }
                        catch (er) {
                            txt = txt + "<td> </td>";
                        }
                    }
                    xx2 = x[i].getElementsByTagName("content");
                    {
                        try {
                            txt = txt + "<td>" + xx2[0].firstChild.nodeValue + "</td>";
                        }
                        catch (er) {
                            txt = txt + "<td> </td>";
                        }
                    }
    
                    xx3 = x[i].getElementsByTagName("date");
                    {
                        try {
                            txt = txt + "<td>" + xx3[0].firstChild.nodeValue + "</td>";
                        }
                        catch (er) {
                            txt = txt + "<td> </td>";
                        }
                    }
                    txt = txt + "</tr>";
                }
                txt = txt + "</table>";
                document.getElementById('showhtml').innerHTML = txt;
    </script>

    页面

    <div id="showhtml">
            <button onclick="loadXMLDoc('1.xml')">Get xml information</button>
    </div>


    1.xml

    <?xml version="1.0" encoding="GBK" ?>
    <news>
    <item>
    <title>this is title</title>
    <content>this is content</content>
    <date>2013-12-10 13:50:44</date>
    </item>
    </news>
  • 相关阅读:
    Windows Phone开发31日谈
    Log4Net(二)
    依赖注入容器Autofac的详解
    Windows Phone 学习教程(一)
    Fiddler教程
    MongoDb笔记(一)
    poj 1144 Network
    poj 3185 The Water Bowls
    poj 1753 Flip Game
    poj 2065 SETI
  • 原文地址:https://www.cnblogs.com/liuyueyingzi/p/3469968.html
Copyright © 2020-2023  润新知