Recently, xml has been used in the project. The requirement is that when users install the product, they first save a series of data into the xml file, and write it to the database when the last step is executed. This minimizes the access to the database, so they have to worry about the compatibility of each browser (sad...)
Enter the main text and is an xml file (createInstal.xml)
<?xml version="1.0" encoding="utf-8"?><info><Item><id description="level" name="1" f_chines="number" t_chines="" english="id" value="1">number</id><levelname description="level" name="" f_chines="level name" t_chines="name" english="Level-Name" value="Level-1">level name</levelname><decrption description="Level" name="" f_chines="Level-Description" english="Level-Description" value="Level">Description</decrption><Tchines description="Level" name="" f_chines="Traditional Chinese" t_chines="Traditional Chinese" english="T-Chinese" value="1">Traditional Chinese</Tchines><english description="Level" name="" f_chines="English name" t_chines="English name" english="English" value="LevelOne">English name</english><6 description="Award" name="106" f_chines="Award" t_chines="Six" english="Worda-of-t" value="a"/><Award eleven description="Award" name="111" f_chines="Award eleven" t_chines="Eleven" english="11" value="0.05"/><Award eleven description="Award" name="112" f_chines="Award eleven" t_chines="Award" name="112" f_chines="Award eleven" t_chines="Award" name="113" f_chines="Award eleven" t_chines="3333" value="0.85"/><Award eleven description="Award" name="101" f_chines="Award one" t_chines="one" english="Aword-of-a" value="0.90"/></Item> </info>
In order to be compatible with IE and FF, write the following functions (loadxml.js):
var is_Ie =false; //Is it an 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 {//Change asynchronous loading of xmldoc.async =false;xmldoc.load(xmlUrl);return xmldoc;}catch (e) {alert(e.message);}returnnull;}//Swap a string in xml document format to 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;}}}}//Get the text of the node and its children function getXmlText(oNode) {if (oNode.text) {//IEreturn oNode.tex;}var sText ="";for (var i =0; i < oNode.childNodes.length; i++) { //Tranquility of the child node if (oNode.childNodes[i].hasChildNodes()) { //Is there a child node sText += getXmlText(oNode.childNodes[i]);} else {sText += oNode[i].childNodes.nodeValue;}}return sText;}//Get the string identification of the node and its children's nodes function getXml(oNode) {if (oNode.xml) {//IEreturn oNode.xml;}var serializer =new XMLSerializer();return serializer.serializeToString(oNode);}//Get the text of the specified node (Note: you can also use oNode.childNodes[0].nodeValue to get the text information of the node, so that you don't need to consider the browser's problem oNodeoNode) function getxmlnodeText(oNode) {if (is_Ie) {return oNode.text;} else {if (oNode.nodeType ==1)return oNode.textContent;}}//get the attribute 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");//Load an xml file var root = document.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], "description");//Get description attribute of this node if(attr != "undefined")//The purpose is to be compatible with FF browser { alert(attr); }}This will ensure compatibility between IE and FF (at present, Google Chrome cannot use this method to be compatible, and it is still to be modified)
Also, let’s talk about two ways to get the xml in FireFox:
JS read XML files in firefox
I searched for the method of "JS reading XML files in firefox" online for a long time, and many of them were asked and no one answered. I saw a bunch of programmers complaining about firefox: "There is no benefit except to exhaust programmers." I got back to the point. Firefox does not support ActiveXObject objects in ie. To get an XML DOM, there are two ways to:
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 an xml object.
2. var oXmlHttp = new XMLHttpRequest();
oXmlHttp.open( "GET", "test.xml", false );
oXmlHttp.send(null);
//oXmlHttp.responseXML is an xml object.
Notice:
1. Firefox parses xml documents
2. Firefox browser and I use textContent to parse the values of different nodes of xml.
3. And it will add "/n" line breaks before and after some hierarchical child nodes (that is, when using childNodes). (I don't know why this is the case when debugging with firebug, so it's best to test the code I've written. If it's 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.
According to the above Firefox situation, I have an example here to avoid using childNodes and achieve compatibility: Click to enter
The above is the implementation method of js operation XML files brought to you by the editor, which is compatible with all contents of IE and FireFox. I hope everyone supports Wulin.com more~