XML technical m...login
XML technical manual
author:php.cn  update time:2022-04-14 15:57:53

XML parser


XML Parser


All modern browsers have built-in XML parsers.

XML parsers convert XML documents into XML DOM objects - objects that can be manipulated through JavaScript.


Parse XML document

The following code snippet parses the XML document into an XML DOM object:

if (window.XMLHttpRequest)
{// 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 parses the 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.


Cross-domain access

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

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


XML DOM

In the next chapter, you will learn how to access XML DOM objects and retrieve data.


php.cn