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

XML Parser

previous next

Most browsers have a built-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 built-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 accessed with JavaScript.

You will learn more about the XML DOM in the next chapter of this tutorial.

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 (elements) and their attributes.

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

Note: When we talk about parsing XML, we often use the term "Nodes" about XML elements.


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 ("note.xml") into the parser:

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

Example explained:

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

var 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 ("note.xml") into the parser:

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

Example explained:

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

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

Example explained:

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


Access Across Domains

For security reasons, modern browsers do 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".


The XML DOM

In the next chapter of this tutorial, you will learn how to access and retrieve data from the XML document object (the XML DOM).


previous next

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