XML reading in different browsers

Source: Internet
Author: User

IE and Firefox read XML in different ways, which also leads to system compatibility issues. The following describes a method that I think is better compatible with IE and Firefox to read XML.

Because IE and Firefox have different attributes in reading XML nodes, we cannot judge whether it is IE or Firefox every time we write a node. This is not only prone to errors, the Code is also very redundant. If we judge IE or Firefox from the very beginning, we need two sets of corresponding code. If the form layout needs to be modified, we need to modify the code in two places, it is also relatively difficult to maintain, and here we load the nodes read by IE and Firefox into the array, and then display them by calling data, which is a good solution.

 

VaR xmlchilddoc; var type = 1; // The default value is Var DATA = new array () in IE browser; // If (navigator. useragent. indexof ("MSIE")> 0) {xmlchilddoc = new activexobject ("Microsoft. xmldom "); // instantiate the DOM object xmlchilddoc. async = false; xmlchilddoc. load (PATH); // load the XML file // window. alert ('ie');} else if (isfirefox = navigator. useragent. indexof ("Firefox")> 0) {type = 2; xmlchilddoc = document. implementation. createdocumen T ("", "", null); // Firefox does not support activexobject xmlchilddoc. async = false; xmlchilddoc. load (PATH);} else {window. alert ('this browser is not recognized now! '); Return;} If (xmlchilddoc) {var nodes; If (type = 1) // judge whether it is iebrowser {If (xmlchilddoc.doc umentelement = NULL) return; // If a node is empty, node = xmlchilddoc.doc umentelement is returned. childnodes; // The node here can be understood as a table in net, so that you can understand for (VAR I = 0; I <node. length; I ++) {var childname = node [I]. attributes [0]. nodetypedvalue; // retrieve the value of the field in line I. This makes it easier to understand var childurl = node [I]. attributes [1]. nodetypedvalue; var childtarget = Node [I]. attributes [2]. nodetypedvalue; data. push ({childname: childname, childurl: childurl, childtarget: childtarget}) ;}} else {var node = xmlchilddoc. getelementsbytagname ("lititle"); For (VAR I = 0; I <node. length; I ++) {var childname = node [I]. attributes [0]. nodevalue; // retrieve the value of the field in line I. This makes it easier for you to understand var childurl = node [I]. attributes [1]. nodevalue; var childtarget = node [I]. attributes [2]. nodevalue; Data. Push ({childname: childname, childurl: childurl, childtarget: childtarget}) ;}} else {window. Alert ("the DOM object is empty and failed! "); Return;} // obtain the root contact var childnodes = xmlchilddoc.doc umentelement. childnodes; STR + = "<Div class = 'menu-list'> <Div class = 'top-line'> </div> <ul class = 'nav-items '> "; for (VAR I = 0; I <data. length; I ++) {STR + = "<li class = 'ceshi move '> <a class ='" + strnum + "'href = javascript: edit ('"+ data [I]. childname + "','" + data [I]. childurl + "','" + data [I]. childtarget + "')"; STR + = "> <SPAN class ='" + path + "'>"; STR + = data [I]. childname; STR + = "</span> </a> </LI>";} HTML + = STR + "</ul> </div> ";

 

Of course, browsers only have firefox and IE, so what should we do if we want to continue to be compatible? The following uses Google and opera as an example.

If we are compatible with other browsers, such as Google, Google, XML reading, and IE Firefox, we need to add judgment when reading

 

else if (navigator.userAgent.indexOf("Chrome") >= 0) {        type = 2;        var oXmlHttp = new XMLHttpRequest();        oXmlHttp.open("GET", path, false);        oXmlHttp.send(null);        xmlChildDoc = oXmlHttp.responseXML;    }

 

If operabrowser is used, add judgment.

(navigator.userAgent.indexOf("Opera") >= 0) 

 

In this way, if we use browser compatibility problems in different places, we can write and judge, and then find the corresponding browser statements on the Internet to solve the problem.

 

Of course, with the increase of browsers, we cannot be compatible with all browsers. The key is how to achieve a balance so that our programs are compatible with mainstream browsers. For some very personalized browsers, we don't have to worry about it. After all, it is a thankless effort to make the system compatible with every browser.

 

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.