You are here: Reference > JavaScript > client-side > xml handling > objects > DOMParser

DOMParser object

Browser support:
9
Provides methods to build XMLDocument objects from XML formatted strings or streams.
Note: The DOMParser object is supported in Internet Explorer from version 9.
In older Internet Explorer versions (and optionally in newer ones), if you want to build an XMLDocument object from a string, create a new XMLDocument object first, then build its contents with the loadXML method. See the examples below for details.

Syntax:

Methods that return the object:
new DOMParser ( )

Possible members:

Methods:
parseFromBuffer
Creates an XMLDocument object from the specified byte array.
parseFromStream
Creates an XMLDocument object from the specified byte stream.
parseFromString
Builds an XMLDocument object from the specified string.

Example HTML code 1:

This example illustrates the use of the DOMParser object:
<head>
    <script type="text/javascript">
        var str = "<root><customer name='John' address='Chicago'></customer></root>";
        function CreateXMLDocument () {
            var xmlDoc = null;
            if (window.DOMParser) {
                var parser = new DOMParser ();
                xmlDoc = parser.parseFromString (str, "text/xml");
            } else if (window.ActiveXObject) {
                xmlDoc = new ActiveXObject ("Microsoft.XMLDOM");
                xmlDoc.async = false;
                xmlDoc.loadXML (str);
            }

            var customerNode = xmlDoc.getElementsByTagName ("customer")[0];
            var customerName = customerNode.getAttribute ("name");
            alert ("The name of the first customer is " + customerName);
        }
    </script>
</head>
<body>
    <button onclick="CreateXMLDocument ();">Create an XML document from a string</button>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This is a more complex example. It uses the XMLHttpRequest object to get an XML document from a server. After the document is received, the responseXML property retrieves an XMLDocument object that conatins the XML document. If the responseXML property is not valid, the responseText property and the DOMParser object are used to get the contents of the response body. See the attached ajax.js file for details.
Code
ajax.js
news.xml
<head>
    <script type="text/javascript" src="ajax.js"></script>
    
    <script type="text/javascript">
        var httpRequest = null;
        
        function SendRequest () {
            if (!httpRequest) {
                httpRequest = CreateHTTPRequestObject ();   // defined in ajax.js
            }
            if (httpRequest) {          
                    // The requested file must be in the same domain that the page is served from.
                var url = "news.xml";
                httpRequest.open ("GET", url, true);    // async
                httpRequest.onreadystatechange = OnStateChange;
                httpRequest.send (null);
            }
        }

        function OnStateChange () {
            if (httpRequest.readyState == 0 || httpRequest.readyState == 4) {
                if (IsRequestSuccessful (httpRequest)) {    // defined in ajax.js
                    FillTable ();
                }
                else {
                    alert ("Operation failed.");
                }
            }
        }


        function FillTable () {
            var xmlDoc = ParseHTTPResponse (httpRequest);   // defined in ajax.js
            if (!xmlDoc)
                return;

            var resTable = document.getElementById ("resTable");
            var xmlNodes = ["title", "description", "pubDate", "link"];

            var itemTags = xmlDoc.getElementsByTagName ("item");
            for (i = 0; i < itemTags.length; i++) {
                resTable.insertRow (i);
                for (j = 0; j < xmlNodes.length; j++) {
                    var recordNode = itemTags[i].getElementsByTagName (xmlNodes[j])[0];
                    resTable.rows[i].insertCell (j);
                    if ('textContent' in recordNode)
                        resTable.rows[i].cells[j].innerHTML = recordNode.textContent;
                    else
                        resTable.rows[i].cells[j].innerHTML = recordNode.text;
                }
            }
        }
    </script>
</head>
<body onload="SendRequest ()">
    <table border="1px">
        <thead style="font-weight: bold;">
            <tr>
                <td>Title</td>
                <td>Description</td>
                <td>PubDate</td>
                <td>Link</td>
            </tr>
        </thead>
        <tbody id="resTable">
        </tbody>
    </table>
</body>
Did you find this example helpful? yes no

External links:

User Contributed Comments

Post Content

Post Content