The implementation of js XML file operations is compatible with IE, FireFox, and xmlfirefox.

Source: Internet
Author: User

The implementation of js XML file operations is compatible with IE, FireFox, and xmlfirefox.

Xml has been used in recent projects. You need to save a series of data to the xml file before installing the product, and write the data to the database only after the last step is executed, this minimizes database access, so you have to struggle with the compatibility issues in various browsers (sadly ....)

The following is an xml file (createInstal. xml)

<? Xml version = "1.0" encoding = "UTF-8"?> <Info> <Item> <id descrption = "level" name = "1" f_chines = "no." t_chines = "commandid" english = "id" value = "1"> no. </id> <levelname descrption = "Level" name = "" f_chines = "Level Name" t_chines = "other names" english = "Level-name" value =" level 1 "> Level name </levelname> <decrption descrption =" Level "name =" "f_chines =" Level Description "t_chines =" Level Description "english =" Level-Description "value =" Level 1 "> description </decrption> <Tchines descrption =" level "name =" "f_chines =" Traditional Chinese "t_chines =" Traditional Chinese "english = "T-Chinese" value = "Hangzhou "> traditional Chinese </Tchines> <english descrption = "level" name = "" f_chines = "english name" t_chines = "english name subtitle "english =" English "value =" LevelOne "> english name </english> <award 6 descrption =" Award "name =" 106 "f_chines =" award 6 "t_chines = ""000000006" english = "Worda-of-t" value = "a"/> <award 11 descrption = "Award" name = "111" f_chines = "award 11" t_chines = "20171000011" english =" 11 "value =" 0.05 "/> <Award 12 descrption =" Award "name =" 112 "f_chines =" Award 12 "t_chines =" "20171000012" english = "2222" value = "0.04"/> <award 13 descrption = "Award" name = "113" f_chines = "award 13" t_chines = "Yellow copyright 13th "english =" 3333 "value =" 0.85 "/> <award 1 descrption =" Award "name =" 101 "f_chines =" award 1 "t_chines =" Copyright 13th ""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 an IE browser if (window. activeXObject) {is_Ie = true;} // load the 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 {// disable asynchronous loading of xmldoc. async = false; xmldoc. load (xmlUrl); return xmldoc;} catch (e) {alert (e. message);} returnnull;} // set Replace a string in xml document format with the 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 function getXmlText (oNode) of the node and Its subnodes) {if (oNode. text) {// IEreturn oNode. tex;} var sText = ""; for (var I = 0; I <oNode. childNodes. length; I ++) {// traverse the subnode if (oNode. childNodes [I]. hasChildNodes () {// whether the subnode sText + = getXmlText (oNode. childNodes [I]);} else {sText + = oNode [I]. childNodes. nodeValue ;}} return sText ;}// obtain the string identifier of the node and Its subnodes. 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 the oNode. childNodes [0]. nodeValue to obtain the node's text information, so you do not need to consider the browser's problem. The node function getxmlnodeText (oNode) {if (is_Ie) {return oNode. text;} else {if (oNode. nodeType = 1) return oNode. textContent ;}} // obtain the attribute value 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 method is as follows:

Var docum = loadxml ("createInstal. xml "); // load an xml file var root = developer.doc umentElement; // 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"); // obtain the descrption attribute of this node if (attr! = "Undefined") // The objective is to be compatible with the FF browser {alert (attr );}}

This ensures the compatibility between IE and FF (Google's browser is currently unable to use this method for compatibility and is still to be modified)

In addition, there are two methods for obtaining xml from FireFox:

Javascript in firefox reads XML files

I searched for "javascript reading XML files in firefox" on the Internet for a long time, and many of them asked no one to answer. A bunch of programmers complained about firefox: "There is no benefit except exhausting programmers .", Let's get down to the truth. Firefox does not support ActiveXObject objects in ie. To obtain an xml dom, you can use either of the following methods:

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.

Note:

1. Firefox parses xml documents

2. Firefox and ie use textContent to parse different node values of xml.

3. In addition, it will add the "\ n" line break before and after some child nodes (that is, when childNodes is used. (I don't know why, this is what it looks like when debugging with firebug, so it is best to test the code that has been written and change the environment), that is to say, the 1st nodes are "\ n ", 2nd nodes are real

The first node. 3rd nodes are \ n, and 4th nodes are the real second node.

Based on the above situation, I have an example here to avoid using childNodes to achieve compatibility:Click to enter

The above is the implementation method of js XML file operations that xiaobian brings to everyone. It is compatible with all content of IE and FireFox. I hope you can support more help ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.