XML Parser in Mozilla, Firefox, Opera and other browser work little differently from Microsoft XML parser. If you try to open the example of the previous chapter with a browser other then the Microsoft Internet Explorer, you might not be able to see the parser output. Reason is the previous example is optimized for the Microsoft Internet Explorer. |
Mozilla’s XML (I’ve used Mozilla XML parser to refer to XML parser of Mozilla, Firefox and Opera browsers) parser supports all the necessary functions to traverse the node tree, access the nodes and their attribute values, insert and delete nodes, and convert the node tree back to XML. |
Creating an instance of the XML Parser in Mozilla: |
var xmlDoc=document.implementation.createDocument(“namespace”,”rootelement”,null); |
The first parameter, namespace, defines the namespace used for the XML document. The second parameter, rootelement , is the XML root element in the XML file. The third parameter, null, is always null because it is not implemented yet. |
Loading an XML document into parser |
The following code fragment loads an existing XML document (“simple_xml.xml”) into Mozillas’ XML parser: |
var xmlDoc=document.implementation.createDocument(“”,”address”,null);
xmlDoc.load(“simple_xml.xml”); |
The second line tells the parser to load an XML document called “simple_xml.xml”. |
<html> <head> <script type=”text/javascript”> var xmlDoc; function createParserInstance() { xmlDoc=document.implementation.createDocument(“”,””,null); xmlDoc.load(“simple_xml.xml”); xmlDoc.onload=loadMSG; } //document.write(xmlDoc.getElementsByTagName(“name”) document.getElementById(“address_str”).innerHTML= xmlDoc.getElementsByTagName(“address_str”)[0].childNodes[0].nodeValue; document.getElementById(“city”).innerHTML= xmlDoc.getElementsByTagName(“city”)[0].childNodes[0].nodeValue; document.getElementById(“state”).innerHTML= xmlDoc.getElementsByTagName(“state”)[0].childNodes[0].nodeValue; document.getElementById(“pin”).innerHTML= xmlDoc.getElementsByTagName(“pin”)[0].childNodes[0].nodeValue; |