What is XML DOM Parser

In this articles i am going to explain about how to use XML DOM parser.
  • 2157

Create a  XML Parser

  • XML DOM object method is create a Parser object in XML.
  • The XML DOM method access interface and define object and properties of all HTML elements.
  • XML DOM define three methods add, delete, replace.
  • An XML parser reads XML, and converts it into an XML DOM object that can be accessed with JavaScript.
  • All browser have built XML Parser
  • javax.xml.parser is use to create parser.

Example:

Example for loading the Document in XML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<!DOCTYPE html>

<html>

<body>

<script type="text/javascript">

    if (window.XMLHttpRequest) {

        xhttp = new XMLHttpRequest();

    }

    else // for IE 5/6

    {

        xhttp = new ActiveXObject("Microsoft.XMLHTTP");

    }

    xhttp.open("GET", "books.xml", false);

    xhttp.send();

    xmlDoc = xhttp.responseXML;

    document.write("XML DOM document loaded.");

</script>

</body>

</html>

Output

parser.jpg

Define the code

  • First we create XML http object and open it.
  • XML http script send the request to the server.
  • book.xml get the data.
  • Set the response as an XML DOM object.
  • Document.write is print the document
  • Internet Explorer uses the load XML() method to parse an XML documents.

Example for loading the String in XML

<!DOCTYPE html>

<html>

<body>

    <h1>

        DOT NET HEAVEN</h1>

    <div>

        <b>To:</b> <span id="to"></span>

        <br />

        <b>From:</b> <span id="from"></span>

        <br />

        <b>Message:</b> <span id="message"></span>

    </div>

    <script type="text/javascript">

        txt = "<note>";

        txt = txt + "<to>Tove</to>";

        txt = txt + "<from>Jani</from>";

        txt = txt + "<heading>Reminder</heading>";

        txt = txt + "<body>please contact to me</body>";

        txt = txt + "</note>";

 

        if (window.DOMParser) {

            parser = new DOMParser();

            xmlDoc = parser.parseFromString(txt, "text/xml");

        }

        else // Internet Explorer

        {

            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

            xmlDoc.async = false;

            xmlDoc.loadXML(txt);

        } 

        document.getElementById("to").innerHTML = xmlDoc.getElementsByTagName("to")[0]

         childNodes[0].nodeValue;

        document.getElementById("from").innerHTML = xmlDoc.getElementsByTagName("from")

         0].childNodes[0].nodeValue;

        document.getElementById("message").innerHTML = xmlDoc.getElementsByTagName

         "body")[0].childNodes[0].nodeValue;

    </script>

</body>

</html>

 

In this example parses an XML string into an XML DOM object and then extracts some info from it with a JavaScript:

 

Output

parser string.jpg

You may also want to read these related articles :

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2019 DotNetHeaven. All rights reserved.