Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中url的xml解析_Javascript_Xml_Parsing_Url - Fatal编程技术网

javascript中url的xml解析

javascript中url的xml解析,javascript,xml,parsing,url,Javascript,Xml,Parsing,Url,我需要一些代码来解析javascript中url中的XML。我尝试了以下代码: 资料来源: <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"> <html> <head><title>Employee Data</title> <script> // This function loads the XML document from the

我需要一些代码来解析javascript中url中的XML。我尝试了以下代码:

资料来源:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<head><title>Employee Data</title>
<script>
    // This function loads the XML document from the specified URL, and when
    // it is fully loaded, passes that document and the url to the specified
    // handler function.  This function works with any XML document

    function loadXML(url, handler) {
        // Use the standard DOM Level 2 technique, if it is supported
        if (document.implementation && document.implementation.createDocument) {
            // Create a new Document object
            var xmldoc = document.implementation.createDocument("", "", null);
            // Specify what should happen when it finishes loading
            xmldoc.onload = function() { handler(xmldoc, url); }
            // And tell it what URL to load
            xmldoc.load(url);
        }
        // Otherwise use Microsoft's proprietary API for Internet Explorer
        else if (window.ActiveXObject) { 
            var xmldoc = new ActiveXObject("Microsoft.XMLDOM");   // Create doc.
            xmldoc.onreadystatechange = function() {              // Specify onload
                if (xmldoc.readyState == 4) handler(xmldoc, url);
            }
            xmldoc.load(url);                                     // Start loading!
        }
    }

    // This function builds an HTML table of employees from data it reads from
    // the XML document it is passed.
    function makeTable(xmldoc, url) {
        // Create a <table> object and insert it into the document.
        var table = document.createElement("table");
        table.setAttribute("border", "1");
        document.body.appendChild(table);

        // Use convenience methods of HTMLTableElement and related interfaces
        // to define a table caption and a header that gives a name to each column.
        var caption = "Employee Data from " + url;
        table.createCaption().appendChild(document.createTextNode(caption));
        var header = table.createTHead();
        var headerrow = header.insertRow(0);
        headerrow.insertCell(0).appendChild(document.createTextNode("Name"));
        headerrow.insertCell(1).appendChild(document.createTextNode("Address"));
        headerrow.insertCell(2).appendChild(document.createTextNode("State"));

        // Now find all <employee> elements in our xmldoc document
        var employees = xmldoc.getElementsByTagName("item");

        // Loop through these employee elements
        for(var i = 0; i < employees.length; i++) {
            // For each employee, get name, job, and salary data using standard DOM
            // methods.  The name comes from an attribute.  The other values are
            // in Text nodes within <job> and <salary> tags.
            var e = employees[i];
            var name = e.getAttribute("name");
            var job = e.getElementsByTagName("address")[0].firstChild.data;
            var salary = e.getElementsByTagName("state")[0].firstChild.data;
            alert(name);
            // Now that we have the employee data, use methods of the table to
            // create a new row and then use the methods of the row to create
            // new cells containing the data as text nodes.
            var row = table.insertRow(i+1);
            row.insertCell(0).appendChild(document.createTextNode(name));
            row.insertCell(1).appendChild(document.createTextNode(job));
            row.insertCell(2).appendChild(document.createTextNode(salary));
        }
    }
</script>
</head>
<!-- 

The body of the document contains no static text; everything is dynamically
generated by the makeTable() function above.  
The onload event handler starts things off by calling loadXML() to load the XML data file.    Note the use of location.search to encode the name of the xml file in the query string.    Load this HTML file with a URL like this: DisplayEmployeeData.html?data.xml
-->
<body onload="loadXML(url, makeTable)">
</body>
</html>

员工数据
//此函数用于从指定的URL加载XML文档,并在
//它已完全加载,将该文档和url传递到指定的
//处理函数。此函数适用于任何XML文档
函数loadXML(url,处理程序){
//如果支持,请使用标准DOM级别2技术
if(document.implementation&&document.implementation.createDocument){
//创建新的文档对象
var xmldoc=document.implementation.createDocument(“,”,null);
//指定加载完成后应执行的操作
xmldoc.onload=function(){handler(xmldoc,url);}
//并告诉它要加载的URL
加载(url);
}
//否则,请使用Microsoft针对Internet Explorer的专有API
如果(window.ActiveXObject){
var xmldoc=newActiveXObject(“Microsoft.XMLDOM”);//创建文档。
xmldoc.onreadystatechange=函数(){//指定onload
if(xmldoc.readyState==4)处理程序(xmldoc,url);
}
加载(url);//开始加载!
}
}
//此函数根据从中读取的数据构建员工的HTML表
//传递给它的XML文档。
函数makeTable(xmldoc,url){
//创建一个对象并将其插入到文档中。
var table=document.createElement(“表”);
表.setAttribute(“边框”、“1”);
document.body.appendChild(表);
//使用HTMLTableElement和相关接口的方便方法
//定义表标题和为每列提供名称的标题。
var caption=“来自”+url的员工数据;
table.createCaption().appendChild(document.createTextNode(caption));
var header=table.createTHead();
var headerrow=header.insertRow(0);
headerrow.insertCell(0.appendChild(document.createTextNode(“Name”));
headerrow.insertCell(1.appendChild(document.createTextNode(“地址”));
headerrow.insertCell(2.appendChild(document.createTextNode(“State”));
//现在在我们的xmldoc文档中找到所有元素
var employees=xmldoc.getElementsByTagName(“项目”);
//循环遍历这些employee元素
对于(变量i=0;i
请查看并重新格式化代码,使其可读。好的,正如allready所做的那样,但是编辑帮助值得一看。您可以用onload事件发布行吗?
Document
对象没有名为
load
的成员函数。对
handler(xmldoc,url)
的引用实际上应该是
makeTable(xmldoc,url)