if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safarixmlhttp=new XMLHttpRequest();}else{// code for IE6, IE5xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}xmlhttp.open("GET","books.xml",false);xmlhttp.send();xmlDoc=xmlhttp.responseXML;
The following code snippet parses an XML string into an XML DOM object:
txt="<bookstore><book>";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 uses the loadXML() method to parse XML strings, while other browsers use the DOMParser object.
For security reasons, modern browsers do not allow cross-domain access.
This means that the web page and the XML file it is trying to load must be on the same server.
In the next section, you'll learn how to access XML DOM objects and retrieve data.