Parsing XML DOM

Most browsers have built-in XML parsers for reading and manipulating XML.

The parser converts XML into objects accessible by JavaScript.

Example

The examples provided by CodeW3C.com are independent of browsers and platforms. These examples can run in all modern browsers.

Load and parse XML file
Load and parse XML string

Parse XML

All modern browsers have built-in XML parsers for reading and manipulating XML.

The parser reads XML into memory and converts it into an XML DOM object accessible by JavaScript.

Microsoft's XML parser is different from the parsers in other browsers. Microsoft's parser supports loading XML files and XML strings (text), while other browsers use separate parsers. However, all parsers contain functions to traverse the XML tree, access, insert, and delete nodes.

In this tutorial, we will explain to you how to create scripts that can run in IE and other browsers.

Load XML through Microsoft's XML parser

Microsoft's XML parser is built into Internet Explorer 5 and higher versions.

The following JavaScript snippet loads the XML document ("books.xml) Loaded into the parser:

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load("books.xml");

Code Explanation:

  • The first line creates an empty Microsoft XML document object
  • The second line closes asynchronous loading, ensuring that the parser does not continue executing the script before the document is fully loaded
  • The third line informs the parser to load the document named "books.xml"

The following JavaScript snippet loads the string named "txt" into the parser:

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(txt);

Note:loadXML() The method is used to load strings (text), while load() Used to load files.

XML parser in Firefox and other browsers

The following JavaScript snippet loads the XML document ("books.xml) Loaded into the parser:

xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.async="false";
xmlDoc.load("books.xml");

Code Explanation:

  • The first line creates an empty XML document object
  • The second line closes asynchronous loading, ensuring that the parser does not continue executing the script before the document is fully loaded
  • The third line informs the parser to load the document named "books.xml"

The following JavaScript snippet loads the string named "txt" into the parser:

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

Code Explanation:

  • The first line creates an empty XML document object
  • The second line informs the parser to load the string named "txt"

Note:Internet Explorer uses loadXML() method to parse XML strings, while other browsers use DOMParser object.

Parsing XML file - an example across browsers

The following example parses an XML document ("books.xml) Load the XML parser:

<html>
<body>
<script type="text/javascript">
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    xmlDoc=document.implementation.createDocument("","",null);
    }
  catch(e) {alert(e.message)}
  }
try 
  {
  xmlDoc.async=false;
  xmlDoc.load("books.xml");
  document.write("xmlDoc is loaded, ready for use");
  }
catch(e) {alert(e.message)}
</script>
</body>
</html>

TIY

Error: Access Across Domains

For security reasons, modern browsers do not allow cross-domain access.

This means that both the web page and the XML file it tries to load must be on the same server.

The XML file opened by the CodeW3C.com example is located on the CodeW3C.com domain.

If you plan to use the above example on your own web page, you must place the XML file on your own server. Otherwise, xmlDoc.load() will produce an error "Access is denied".

Parsing XML string - an example across browsers

The following code loads and parses an XML string:

<html>
<body>
<script type="text/javascript">
text="<bookstore>"
text=text+"<book>";
text=text+"<title>Harry Potter</title>";
text=text+"<author>J K. Rowling</author>";
text=text+"<year>2005</year>";
text=text+"</book>";
text=text+"</bookstore>";
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(text);
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    parser=new DOMParser();
    xmlDoc=parser.parseFromString(text,"text/xml");
    }
  catch(e) {alert(e.message)}
  }
document.write("xmlDoc is loaded, ready for use");
</script>
</body>
</html>

TIY

Note:Internet Explorer uses the loadXML() method to parse XML strings, while other browsers use DOMParser Object.