Scholar Repository
Home>Manual>XML DOM Parser

XML DOM Parser

From http://www.w3schools.com (Copyright Refsnes Data)

Parsing the XML DOM

prev next


Most browsers have a build-in XML parser to read and manipulate XML.

The parser converts XML into a JavaScript accessible object.


Examples

W3Schools examples are browser and platform independent. These examples work in all modern browsers.

Load and parse an XML file

Load and parse an XML string


Parsing XML

All modern browsers have a build-in XML parser that can be used to read and manipulate XML.

The parser reads XML into memory and converts it into an XML DOM object that can be accesses with JavaScript.

There are some differences between Microsoft's XML parser and the parsers used in other browsers. The Microsoft parser supports loading of both XML files and XML strings (text), while other browsers use separate parsers. However, all parsers contain functions to traverse XML trees, access, insert, and delete nodes.

In this tutorial we will show you how to create scripts that will work in both Internet Explorer and other browsers.


Loading XML with Microsoft's XML Parser

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

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

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

Code explained:

  • The first line creates an empty Microsoft XML document object.
  • The second line turns off asynchronized loading, to make sure that the parser will not continue execution of the script before the document is fully loaded.
  • The third line tells the parser to load an XML document called "books.xml".

The following JavaScript fragment loads a string called txt into the parser:

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

Note: The loadXML() method is used for loading strings (text), load() is used for loading files.


XML Parser in Firefox and Other Browsers

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

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

Code explained:

  • The first line creates an empty XML document object.
  • The second line turns off asynchronized loading, to make sure that the parser will not continue execution of the script before the document is fully loaded.
  • The third line tells the parser to load an XML document called "books.xml".

The following JavaScript fragment loads a string called txt into the parser:

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

Code explained:

  • The first line creates an empty XML document object.
  • The second line tells the parser to load a string called txt.

Note: Internet Explorer uses the loadXML() method to parse an XML string, while other browsers uses the DOMParser object.


Parsing an XML File - A Cross browser Example

The following example loads an XML document ("books.xml") into 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>

Try it yourself


Error: Access Across Domains

For security reasons, modern browsers does not allow access across domains.

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

The examples on W3Schools all open XML files located on the W3Schools domain.

If you want to use the example above on one of your web pages, the XML files you load must be located on your own server. Otherwise the xmlDoc.load() method, will generate the error "Access is denied".


Parsing an XML String - A Cross browser Example

The following code loads and parses an XML string:

<html>
<body>
<script type="text/javascript">
text="<bookstore>"
text=text+"<book>";
text=text+"<title>Everyday Italian</title>";
text=text+"<author>Giada De Laurentiis</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>

Try it yourself

Note: Internet Explorer uses the loadXML() method to parse an XML string, while other browsers uses the DOMParser object.


prev next

From http://www.w3schools.com (Copyright Refsnes Data)

Home>Manual>XML DOM Parser