XML file operations in js are compatible with IE and FireFox

Source: Internet
Author: User

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 of various browsers (sad ....)

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 = "" name = "" f_chines = "" t_chines = "" 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 Description 1"> Description </decrption>
<Tchines descrption = "level" name = "" f_chines = "Traditional Chinese" t_chines = "Traditional Chinese" english = "T-Chinese" value = "Others"> traditional Chinese chinese </Tchines>
<English descrption = "level" name = "" f_chines = "english name" t_chines = "English name" english name = "english" value = "LevelOne"> english name </English>
<Award 6 descrption = "Award" name = "106" f_chines = "award 6" t_chines = "000000006" english = "Worda-of-t" value = "" />
<Award 11 descrption = "Award" name = "111" f_chines = "award 11" t_chines = "201710000000011" english = "11" value = "0.05"/>
<Award 12 descrption = "Award" name = "112" f_chines = "Award 12" t_chines = "0000000012" english = "2222" value = "0.04"/>
<Award 13 descrption = "Award" name = "113" f_chines = "award 13" t_chines = "0000000013" english = "3333" value = "0.85"/>
<Award 1 descrption = "Award" name = "101" f_chines = "award 1" t_chines = "Copyright Anyi" 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 the browser is an IE browser
If (window. ActiveXObject ){
Is_Ie = true;
}
// Load xml documents compatible with multiple browsers
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
Xmldoc. async = false;
Xmldoc. load (xmlUrl );
Return xmldoc;
}
Catch (e ){
Alert (e. message );
}
Returnnull;
}
// Replace a string in xml document format with an 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;
}
}
}
}
// Obtain the text of the node and Its subnodes
Function getXmlText (oNode ){
If (oNode. text) {// IE
Return oNode. tex;
}
Var sText = "";
For (var I = 0; I <oNode. childNodes. length; I ++) {// traverse subnodes
If (oNode. childNodes [I]. hasChildNodes () {// whether a subnode exists
SText + = getXmlText (oNode. childNodes [I]);
} Else {
SText + = oNode [I]. childNodes. nodeValue;
}
}
Return sText;
}

// Obtain the string ID of the node and Its subnodes
Function getXml (oNode ){
If (oNode. xml) {// IE
Return oNode. xml;
}
Var serializer = new XMLSerializer ();
Return serializer. serializeToString (oNode );

}
// Obtain the text of the specified Node
Function getxmlnodeText (oNode ){
If (is_Ie ){
Return oNode. text;
} Else {
If (oNode. nodeType = 1)
Return oNode. textContent;
}
}
// Obtain 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 method is as follows:

Var docum = loadxml ("createInstal. xml"); // load an xml file

Var root = cmd.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)

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.