JS load XML file as a new army of software developers, I began to dream with a navy qualification, and began to smear on ' a piece of paper ' with the qualifications of a painting man. Where's the job?? Where do you work??? Find an interview today: The attachment has a city list of city.xml files, now you need to parse the file through JavaScript script, and then restore the city list to a drop-down list box, when you select the drop-down list box, the corresponding text box displays the city information (interface layout refer to Test1.html can also be defined by yourself).
On the Internet to check the information of the day, racked his brains to try the online predecessors of various methods, why each other success of the case, to their own here is a very different??? After a tangled day, after checking the internet for a day's information, summarize your first blog post here.
With the disintegration of JS founding company, JS in the late and not with the browser of the development of a lot of very good maintenance, in the compatibility of a series of problems. Then JS load XML with the use of different browsers, there are several situations. (And maybe, just as a rookie I don't know OH)
1. Applicable to IE lower version IE9 (should be very few now).
functionLoadxml () {//the MSXML libraries within the IE9 version have the following three versions, and different versions use different libraries. varVersion = [ ' MSXML2. DomDocument6.0 ', ' MSXML2. DomDocument3.0 ', ' MSXML2. DomDocument ' ]; for(vari=0;i<version.length;i++){ Try { //Create a performer that loads XML varXmlDom =NewActiveXObject (); returnXmlDom; } Catch(e) {}}ThrowError ("Your system or browser does not support MSXML");}//load a string in XML formatvarXmlDom =Loadxml (); XMLDOM.loadXML (' <root><user>aaa</user></root> ');//get user's test valuevarUsertext = Xmldom.getelementsbytagname ("user") [0].firstchild.nodevalue;//loading XML external filesvarXmlDom2 =Loadxml (); Xmldom2.load ("Demo.xml");//adding nodes to XML textvarXMLDOM3 =Loadxml (); Xmldom3.load ("Demo.xml");varBBB = xmldom3.createelement ("BBB");varroot=Xmldom3.documentelement;root.appendchild (BBB);//The server side uses asynchronous loading by default. If the asynchronous load is not finished loading, the data will be used to report an error//using synchronous loading, the default is true. Synchronous loading can occur in the event of suspended animation. Xmldom.async=false;//methods for using asynchronous loadingXmldom.asybc=trueXmldom.onreadystatechange=function(){ if(xmldom.readystate==4){ if(xmldom.parseerror.errorcode==4){ //no errors, execute the post-load program .....alert (xmldom.xml); }Else{ Throw NewError ("Wrong line number:" +xmldom.parseerror.line+ "error code:" +xmldom.parseerror.errorcode+ "Cause of Error:" +XmlDom.parseError.reason); }}}xmldom.load ("Demo.xml");
View Code
2. Browsers for Firefox and new versions of Opera support load () method loading external XML file
//the Load method is only available for Firefox and the new version of Opera browserfunctionLoadxml () {//Create Xmldom object (DOM2 level XML) varXmlDom = Document.implementation.createDocument ("", "root",NULL); //get the contents of an XML tag 2 ways //alert (xmlDom.documentElement.tagName) //Alert (xmldom.getelementsbytagname ("root") [0].tagname]; //Note: The DOM2 level cannot create a document from a simple string. //the external project is loaded with the load () method file. Note Synchronous asynchronous /*//synchronous loading xmldom.async=false; Xmldom.load ("City.xml"); Load external XML file, DOM2 level does not have. xml to serialize alert (Xmldom.getelementsbytagname ("City") [0].tagname); Alert (Xmldom.getelementsbytagname ("Description") [0].firstchild.nodevalue); Alert (Xmldom.getelementsbytagname ("Description") [0].textcontent); Gets the property value alert (Xmldom.getelementsbytagname ("City") [0].getattribute ("Name")); */ //asynchronous loading, using XMLDOM to invoke onloadXmldom.async =true; Xmldom.onload=function() {alert (Xmldom.getelementsbytagname ("Description") [0].textcontent); Alert (Xmldom.getelementsbytagname ("City") [0].getattribute ("Name")); } xmldom.load ("City.xml"); }
View Code
3. Simulate the Loadxml () method, you can use the easy-to-create XML string (new Domparser () object), most browsers support
functionDome () {//simulates the Loadxml () method, which can be easily created using an XML string varXmlparser =NewDomparser ();//Create a Domparser object varXmlstr = "<root><user name= ' aaa ' >Cao</user></root>"; varXmlDom = xmlparser.parsefromstring (Xmlstr, "Text/xml"); Alert (Xmldom.getelementsbytagname ("User") [0].getattribute ("Name"))); Alert (Xmldom.getelementsbytagname ("User") [0].textcontent); //simulating. XML Serialization Strings varSerializer =NewXMLSerializer (); varXML =serializer.serializetostring (XmlDom); varErrors = Xmldom.getelementsbytagname ("User"); if(errors.length==0){ //executes subsequent code, such asalert (XML); }Else{ Throw NewError ("Message:" +errors[0].textcontent); }}dome ();
View Code
4. Multi-browser access to XML strings, non-XML files
functionDemo (xmlstr) {varXmlparser =NewDomparser (); varXmldom=NULL; if(typeofWindow. domparser!= ' undefined ') {XmlDom= Xmlparser.parsefromstring (Xmlstr, "Text/xml"); varErrors = Xmldom.getelementsbytagname ("ParserError"); if(errors.length>0){ Throw NewError ("Message:" +errors[0].textcontent); } }Else if(typeofWindow. activexobject!= ' undefined '){ //the MSXML libraries within the IE9 version have the following three versions, and different versions use different libraries. varVersion = [ ' MSXML2. DomDocument6.0 ', ' MSXML2. DomDocument3.0 ', ' MSXML2. DomDocument ' ]; for(vari=0;i<version.length;i++){ Try { //Create a performer that loads XML varXmlDom =NewActiveXObject (version); } Catch(e) {}} xmldom.loadxml (XMLSTR); if(typeofXmldom.parseerror!=0){ ThrowError ("Message:" +XmlDom.parseError.reason); } returnXmlDom; }Else{ ThrowError ("Your system or browser does not support XML DOM objects"); } returnXmlDom;}varXmlstr = "<root><user>Cao<user></root>";varXmlDom =demo (XMLSTR); alert (xmlDom);
View Code
5. Applicable jqusery load XML file
$(function() {$.ajax ({URL:' City.xml ', type:' GET ', DataType:' XML ', timeout:1000, Cache:false, Error:function(XML) {alert ("Error loading file!" "); }, success:getxmldom}); });functiongetxmldom (XML) {varD = $ (XML). Find ("City"); for(vari=0;i<d.length;i++){ varName = $ (D[i]). attr ("name"); varText = $ (d[i]). Children ("Description"). text (); } }
View Code
Category: JS
The Load method applies only to Firefox and the new version of Opera browser function Loadxml () {//Create XmlDom object (Dom2 level XML) var XmlDom = Document.implementation.createDocument ("", "root", null);//Get the contents of the XML tag 2 methods//alert (XmlDom.documentElement.tagName) Alert (xmldom.getelementsbytagname ("root") [0].tagname];//NOTE: The DOM2 level cannot create a document from a simple string. The external project is loaded with the load () method file. Note Synchronous asynchronous/*//synchronous load Xmldom.async=false;xmldom.load ("City.xml"); Load external XML file, DOM2 level does not have. xml to serialize alert (Xmldom.getelementsbytagname ("City") [0].tagname); Alert ( Xmldom.getelementsbytagname ("Description") [0].firstchild.nodevalue); Alert (Xmldom.getelementsbytagname (" Description ") [0].textcontent];//Get the property value alert (Xmldom.getelementsbytagname (" City ") [0].getattribute (" Name ")); */// Asynchronous loading, call onload with xmlDom Xmldom.async = True;xmldom.onload=function () {Alert (Xmldom.getelementsbytagname (" Description ") [0].textcontent]; Alert (Xmldom.getelementsbytagname (" City ") [0].getattribute (" Name "));} Xmldom.load ("City.xml"); }
Function Dome () {///simulate Loadxml () method, you can use the simple XML string created by var xmlparser = new Domparser ();//Create Domparser object var xmlstr = "<root ><user name= ' aaa ' >Cao</user></root> '; var xmlDom = xmlparser.parsefromstring (Xmlstr, "Text/xml" Alert (Xmldom.getelementsbytagname ("user") [0].getattribute ("Name")); Alert (Xmldom.getelementsbytagname ("user") [0].textcontent];//Analog. XML Serialization string var serializer = new XMLSerializer (); var xml = serializer.serializetostring (XmlDom); var errors = xmldom.getelementsbytagname ("user"); if (errors.length==0) {//executes subsequent code, such as alert (XML);} Else{throw New Error ("error message:" +errors[0].textcontent);}} Dome ();
JS loading xml file Xin