Implementation of js Operating XML File Compatible with IE and FireFox
- 2021-07-01 06:15:34
- OfStack
Recently, xml is used in the project. The requirement is that when users install products, they first save 1 series of data to xml files, and then write them to the database when they execute the last step. This minimizes the access to the database, so they have to struggle with the compatibility of various browsers (sad....)
Below the body is an xml file (createInstal. xml)
<?xml version="1.0" encoding="utf-8"?>
<info>
<Item>
<id descrption=" Level " name="1" f_chines=" Numbering " t_chines=" In fact, in fact, the " english="id" value="1"> Numbering </id>
<levelname descrption=" Level " name="" f_chines=" Level name " t_chines=" Name " english="Level-Name" value=" Level 1"> Level name </levelname>
<decrption descrption=" Level " name="" f_chines=" Level description " t_chines=" Describe " english="Level-Description" value=" Level description 1"> Describe </decrption>
<Tchines descrption=" Level " name="" f_chines=" Traditional Chinese " t_chines=" Complex Chinese " english="T-Chinese" value=" In fact, in fact, the 1"> Traditional Chinese </Tchines>
<english descrption=" Level " name="" f_chines=" English name " t_chines=" English name " english="English" value="LevelOne"> English name </english>
< Awards 6 descrption=" Awards " name="106" f_chines=" Awards 6" t_chines=" In fact, in fact, the 6" english="Worda-of-t" value="a"/>
< Awards 101 descrption=" Awards " name="111" f_chines=" Awards 101" t_chines=" In fact, in fact, the 101" english="11" value="0.05"/>
< Awards 102 descrption=" Awards " name="112" f_chines=" Awards 102" t_chines=" In fact, in fact, the 102" english="2222" value="0.04"/>
< Awards 103 descrption=" Awards " name="113" f_chines=" Awards 103" t_chines=" In fact, in fact, the 103" english="3333" value="0.85"/>
< Awards 1 descrption=" Awards " name="101" f_chines=" Awards 1" t_chines=" In fact, in fact, the 1" english="Aword-of-a" value="0.90"/>
</Item>
</info>
To be compatible with IE and FF, write the following functions (loadxml. js):
var is_Ie =false; // Whether it is IE Browser
if (window.ActiveXObject) {
is_Ie =true;
}
// Load multi-browser compatible xml Document
function loadXml(xmlUrl) {
var xmldoc =null;
try {
xmldoc =new ActiveXObject("Microsoft.XMLDOM");
}
catch (e) {
try {
xmldoc = document.implementation.createDocument("", "", null);
} catch (e) {
alert(e.message);
}
}
try {
// Turn off asynchronous loading
xmldoc.async =false;
xmldoc.load(xmlUrl);
return xmldoc;
}
catch (e) {
alert(e.message);
}
returnnull;
}
// Will 1 A xml The string in document format is replaced by xml Document
function createXml(xmlText) {
if (!xmlText) {
returnnull;
try {
var xmldocm =new ActiveXObject("Microsoft.XMLDOM");
xmldocm.loadXML(xmlText);
return xmldocm;
}
catch (e) {
try {
returnnew DOMParse().parseFromString(xmlText, "text/xml");
}
catch (e) {
returnnull;
}
}
}
}
// Gets the text of a node and its children
function getXmlText(oNode) {
if (oNode.text) {//IE
return oNode.tex;
}
var sText ="";
for (var i =0; i < oNode.childNodes.length; i++) { // Traversing child nodes
if (oNode.childNodes[i].hasChildNodes()) { // Whether there are child nodes
sText += getXmlText(oNode.childNodes[i]);
} else {
sText += oNode[i].childNodes.nodeValue;
}
}
return sText;
}
// Gets the string identity of the node and its children
function getXml(oNode) {
if (oNode.xml) {//IE
return oNode.xml;
}
var serializer =new XMLSerializer();
return serializer.serializeToString(oNode);
}
// Gets the text of the specified node ( Note: You can also use the oNode.childNodes[0].nodeValue To get the text information of the node, so that you don't have to consider the browser problem oNodeoNode)
function getxmlnodeText(oNode) {
if (is_Ie) {
return oNode.text;
} else {
if (oNode.nodeType ==1)
return oNode.textContent;
}
}
// Gets the property value of the specified node
function getxmlnodeattribute(oNode, attrName) {
if (is_Ie) {
return oNode.getAttribute(attrName);
} else {
if (oNode.nodeType ==1|| oNode.nodeType =="1")return oNode.attributes[attrName].value;return"undefined";}}
ok, IE and FF are no longer problems. The specific operation methods are as follows:
var docum = loadxml("createInstal.xml");// Loading 1 A xml Documents
var root = docum.documentElement;// Root node
var nodelist = root.getElementsByTagName("Items");
for(var i=0;i<nodelist[0].childNodes.length;i++)
{
var attr = getxmlnodeattribute(nodeList[0].childNodes[i], "descrption");// Object for this node descrption Attribute
if(attr != "undefined")// The purpose is compatibility FF Browser
{
alert(attr);
}
}
This will ensure the compatibility of IE and FF. (There is no way for Google Browser to be compatible in this way at present, and it needs to be modified.)
In addition, let's talk about two methods for FireFox to obtain xml:
JS in firefox reads XML file
Search on the Internet "firefox in JS read XML file" method, looking for half a day, many are asked no one answered. Seeing a bunch of programmers complaining about firefox: "There is nothing good but exhausting programmers." , to get down to business. The ActiveXObject object in ie is not supported by firefox. There are two ways to get an XML DOM:
1. document. implementation. createDocument (",", null);
2. window. XMLHttpRequest
Example: 1, var dom = document. implementation. createDocument ("", "", null);
dom.async=false;
dom. load ("test. xml"); //dom is the xml object.
2. var oXmlHttp = new XMLHttpRequest ();
oXmlHttp.open( "GET", "test.xml", false ) ;
oXmlHttp.send(null) ;
//oXmlHttp. responseXML is the xml object.
Note:
1. Firefox parses xml document
2. Firefox browser and ie use textContent to analyze the value of xml.
3, and he adds a "\ n" newline character before and after the child node in some hierarchies (that is, when childNodes is used). (I don't know why, this is what it looks like when debugging with firebug, so the code I wrote is best tested under 1, and it is wrong to change the environment), that is to say, the first node is "\ n", and the second node is the real one
The first node. The third node is "\ n", and the fourth node is the real second node.
Based on the Firefox case above, I have here an example that avoids using childNodes and achieves compatibility: click to enter