XML Parser
Most browsers have a build-in XML parser to read and manipulate XML. The parser
reads XML into memory and converts it into an XML DOM object. There are some
differences between Microsoft's XML parser and the parsers used in other browsers.
Microsoft's XML parser is built into Internet Explorer 5 and higher. The Microsoft
parser supports loading of both XML files and XML strings (text), while other
browsers use separate parsers. I will show you how to create scripts that will
work in both Internet Explorer and other browsers.
The following is an example that Javascript using Microsoft's XML parser to
load an XML document ("gsp.xml") into the parser:
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load("gsp.xml");
In the script above, 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 "gsp.xml".
If the input is not an XML file, but a text string, then you need to change
the third line to::
xmlDoc.loadXML(txt);
So the script should be:
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(txt);
To use XML parser in Firefox and other browsers, we should use the following
script:
var xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.async="false";
xmlDoc.load("gps.xml");
If the input is not an XML file, but a text string, then you need to use a
script like this::
var parser=new DOMParser();
var doc=parser.parseFromString(txt,"text/xml");
|