|
IE Supported Code |
Firefox Supported Code |
| Creating XML DOM object |
Creating XML DOM object |
| xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); |
xmlDoc = document.implementation.createDocument("","doc",null); |
| Make sure before loading the XML file you use the following lines of code |
It is the same for Firefox |
| xmlDoc.async = false;xmlDoc.preserveWhiteSpace=true; |
xmlDoc.async = false;xmlDoc.preserveWhiteSpace=true; |
| Loading an XML file |
Loading an XML file |
| xmlDoc .load("XML Path"); |
xmlDoc.onload=function (){};xmlDoc .load("XML Path"); |
| |
Here Firefox does not support loading of XML directly. So we should use a dummy function and then load XML. |
| Selecting Single Node in an XML with conditions |
Selecting Single Node in an XML |
| xmlNode=xmlDoc.selectSingleNode("//Path[@Attribute='condition']/remaining path"); |
selectSingleNode is not supported in Firefox. We can use XML Path to do our job as given below. |
| |
function SelectSingleNode(xmlDoc, elementPath){ if (document.implementation && document.implementation.createDocument) { var nodes=document.evaluate(elementPath, xmlDoc, null, XPathResult.ANY_TYPE, null);var results=nodes.iterateNext();return results; } } |
| |
To the above function we need to pass XML and the Element path like "//Path[@Attribute='condition']/remaining path" and access it as below |
| |
xmlNode=SelectSingleNode(xmlDoc ,"//Path[@Attribute='condition']/remaining path"); |
| For getting value of particular Attribute |
For getting value of particular Attribute |
| xmlNode.getAttribute("Attribute Name"); |
Here we can use the code given below |
| |
xmlNode.attributes["Attribute Name"].value; |
| To access the Text of the Single Node selected |
To access the Text of the Single Node selected |
| xmlNode.text; |
xmlNode.childNodes[0].nodeValue; |
| Selecting Nodes list based on condition |
Selecting Nodes list based on condition |
| xmlNodes= xmlDoc.selectNodes(“Node name Path [@attribute=’condition’]”) For ex: var xmlNodes = xmlDoc.selectNodes("//xNode[@xid='test']"); |
xmlNodes=xmlDoc.getElementsByTagName[“Node name Path”];For ex: var xmlNodes = xmlDoc.getElementsByTagName[“xNode”]; |
| Iterating through the Nodes List and selecting value of a particular Attribute |
Iterating through the Nodes List and selecting value of a particular Attribute |
| for(var n=0; n< xmlNodes.length; n++){var xid= xmlNodes (n).getAttribute("xid");} xmlNodes contains only the Nodes which has the Attribute xid and which has value ‘test’. If the Attribute value = ‘test’, then we are iterating through that Nodes only and retrieving the value of xid. |
Here xmlNodes contains all the list of Nodes which are having the Node Name “xNode”. for(var n=0; n< xmlNodes.length; n++){ // For getting all the Attributes of the Node selected var getXmlAttributes = xmlDoc.getElementsByTagName("Node name ")[n].attributes; // For selecting Attribute value based on condition, we should first get the Attributes List which contains the Attribute we need as below. var selectXmlAttribute = getXmlAttributes.getNamedItem("xid").value; // For retrieving the value of the Attribute based on Condition. if (selectXmlAttribute =="test") { var xid= getXmlAttributes.getNamedItem("xid").value;} |
| IE supports both () and [] braces while using document object |
Firefox supports only [] braces while using document object. |
| Ex: document.forms(0); or document.forms[0]; |
Ex: or document.forms[0]; |
| Use document.getElementById[“”] instead of document.all – Both are supported in IE document.all is IE specific |
|
| In script tag never forget to mention type=”text/javascript”, as now a days all browsers implicitly know and support Java Script |
For ex: <script language =”javascript” type = ”text/javascript”></script> |
| Don’t close the script tag like <script language =”javascript” type = ”text/javascript”/> as some browsers like IE loads the first script, and then continues to look for a closing </script> tag. |
Use:<script language =”javascript” type = ”text/javascript”></script> |