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

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags ajax browsers can directly copy create data different displays

Web page effects load XML file multiple methods (compatible Ff,ie6+,opera)
Below we provide three kinds of JS loading XML documents, each load XML file method is different, but the principle is the same, the best is the third section, the instance is compatible with multiple browsers, the second instance of the, copy down can be directly used
Method One

var xmldoc;
Try{//ie
Create an empty Microsoft XML Document Object
Xmldoc=new ActiveXObject ("Microsoft.XMLDOM");
}catch (e) {
Try{//ff
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

<html xmlns= "http://www.w3.org/1999/xhtml" >


<head>


<TITLE>JS loads XML and displays it </title>


<script type= "text/web Effects" >


var xmlhttp;


function GetData ()


    {


//Create 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 returned data


var Data=xmlhttp.responsetext;


//Display results


document.getElementById ("Divlist"). Innerhtml=data;


       }


    }


</script>


</head>


<body>


<table style= "Text-align:center" >


<tr>


<TD style= "Text-align:center" >


displays data obtained from XML files </td>


</tr>


<tr>


<td style= "Text-align:center" >


<input id= "button1" type= "button" value= "Get Table" onclick= "GetData ()"/></td>


</tr>


<tr>


<td style= "Text-align:center" >


<div id= "Divlist" >


</div>


</td>


</tr>


</table>

</body>
</html>

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;
}

Related Article

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.