JS load XML file multiple methods (compatible Ff,ie6+,opera)

Source: Internet
Author: User

Method One

var xmldoc;
Try{//ie
Create an empty Microsoft XML Document Object
Xmldoc=new ActiveXObject ("Microsoft.XMLDOM");
}catch (e) {
Try{//ff
To create an empty XML Document object
Xmldoc=document.implementation.createdocument ("", "", null);
}catch (e) {
alert (e.message);
}
}
try{
Turns off asynchronous loading, which ensures that the parser does not continue to execute scripts until the document is fully loaded
Xmldoc.async=false;
Load XML document
Xmldoc.load (Xml_file);
}catch (e) {
alert (e.message);
return false;
}
return xmldoc;

Method Two, JS loads the XML and displays it

&LT;TITLE&GT;JS loads XML and displays it </title>
<script type= "text/web Effects" >
var xmlhttp;
function GetData ()
{
To create an asynchronous object
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
Load server-Note no parameters
Xmlhttp.open ("Get", "XMLFile.xml", True)
Asynchronous Object Event Hooks
Xmlhttp.onreadystatechange=statechange;
Send request-no parameters
Xmlhttp.send (NULL);
}
function StateChange ()
{
if (xmlhttp.readystate==4 && xmlhttp.status==200)
{
Get all the returned data
var Data=xmlhttp.responsetext;
Show results
document.getElementById ("Divlist"). Innerhtml=data;
}
}
</script>
<body>
<table style= "Text-align:center" >
<tr>
&LT;TD style= "Text-align:center" >
Display data obtained from an XML file </td>
</tr>
<tr>
&LT;TD style= "Text-align:center" >
<input id= "button1" type= "button" value= "Get Table" onclick= "GetData ()"/></td>
</tr>
<tr>
&LT;TD style= "Text-align:center" >
<div id= "Divlist" >
</div>
</td>
</tr>
</table>

</body>


Method Three supports Ff,ie6+,opera

function getxmldocument (file) {
var xmldoc;
try{//internet Explorer
   xmldoc=new activexobject ("Microsoft.XMLDOM");
}
catch (e) {
   try{/*firefox, Mozilla, Opera, etc.*/
    xmldoc=document. Implementation.createdocument ("", "", null);
  }
   catch (e) {
    alert (e.message);
    return;
  }
}
Xmldoc.async=false;
Xmldoc.load (URL);
Return xmldoc;
}

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.