Javascript-compatible methods for parsing XML document data in various browsers-javascript skills

Source: Internet
Author: User
This article mainly introduces how to use javascript to parse XML file data compatible with various browsers. it involves the parsing skills of javascript to XML files and makes relevant adjustments based on various browsers, which has good compatibility, for more information about how to use JavaScript to parse XML document data, see the following example. Share it with you for your reference. The specific analysis is as follows:

Many documents on the website that parse XML documents using JS are more or less problematic,

The following code is used to parse XML documents and be compatible with various browsers.

ParseXMLDOM. js code:

/** Parse XML documents in pure JS (compatible with various browsers) */function parseXMLDOM () {var _ browserType = ""; var _ xmlFile = ""; var _ XmlDom = null; return {"getBrowserType": function () {return _ browserType;}, "setBrowserType": function (browserType) {_ browserType = browserType;}, "getXmlFile": function () {return _ xmlFile;}, "setXmlFile": function (xmlFile) {_ xmlFile = xmlFile;}, "getXmlDom": function () {return _ XmlDom ;}," SetXmlDom ": function (XmlDom) {_ XmlDom = XmlDom;}," getBrowserType ": function () {var browserType =" "; if (navigator. userAgent. indexOf ("MSIE ")! =-1) {browserType = "IE";} else if (navigator. userAgent. indexOf ("Chrome ")! =-1) {browserType = "Chrome";} else if (navigator. userAgent. indexOf ("Firefox ")! =-1) {browserType = "Firefox"} return browserType;}, "createXmlDom": function (xmlDom) {if (this. getBrowserType () = "IE") {// iebrowser xmlDom = new ActiveXObject ('Microsoft. XMLDOM '); xmlDom. async = false; xmlDom. load (this. getXmlFile ();} else {var xmlhttp = new XMLHttpRequest (); xmlhttp. open ("GET", this. getXmlFile (), false); xmlhttp. send (null); xmlDom = xmlhttp. responseXML;} return xmlDom;}, "parse XMLDOMInfo ": function () {var xmlDom = this. getXmlDom (); if (this. getBrowserType () = "IE") {var bookObj = xmlDom. selectNodes ("books/book"); if (typeof (bookObj )! = "Undifined") {var strHtml = ""; for (var I = 0; I <bookObj. length; I ++) {strHtml + = bookObj [I]. selectSingleNode ("isbn "). text; strHtml + = ""; strHtml + = bookObj [I]. selectSingleNode ("price "). text; strHtml + = ""; strHtml + = bookObj [I]. selectSingleNode ("title "). text; if (I! = BookObj. length-1) {strHtml + ="
";}}} Else {var book = xmlDom. getElementsByTagName ("book"); var strHtml = ""; for (var I = 0; I <book. length; I ++) {strHtml + = book [I]. getElementsByTagName ("isbn") [0]. textContent; strHtml + = ""; strHtml + = ""; strHtml + = book [I]. getElementsByTagName ("price") [0]. textContent; strHtml + = ""; strHtml + = book [I]. getElementsByTagName ("title") [0]. textContent; if (I! = Book. length-1) {strHtml + ="
";}}} Document. getElementById ("msg "). innerHTML = strHtml ;}} window. onload = function () {var parseObj = new parseXMLDOM (); // Set the browser type parseObj. setBrowserType (parseObj. getBrowserType (); // sets the file path parseObj. setXmlFile ("test. xml "); // create XMLDOM parseObj. setXmlDom (parseObj. createXmlDom (null); // Parse XMLDOM parseObj. parseXMLDOMInfo ();}

Index.html code:

      
    Parse data in XML documents using JS (compatible with all browsers)   

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.