XML parser
All modern browsers have built-in XML parser.
XML parser the XML document into XML DOM object - the object can be operated via JavaScript.
Parsing XML documents
The following code snippet to parse XML documents to XML DOM object:
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","books.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
Parse XML string
The following code snippet to the XML string parsing XML DOM object:
txt=txt+"<title>Everyday Italian</title>";
txt=txt+"<author>Giada De Laurentiis</author>";
txt=txt+"<year>2005</year>";
txt=txt+"</book></bookstore>";
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);
}
NOTE: Internet Explorer use loadXML () method to parse an XML string, while other browsers use DOMParser object.
Cross-domain access
For security reasons, modern browsers do not allow cross-domain access.
This means that it tries to load Web pages and XML files, must be located on the same server.
XML DOM
In the next chapter, you will learn how to access the XML DOM object and retrieve data.