开发者

xml parsing from url in javascript

I need some code for parsing XML from a url in javascript. I tried following code:

Source:

<!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, pas开发者_如何学编程ses 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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜