JavaScript get XML data with sample screenshot _javascript tips

Source: Internet
Author: User
Tags parse error
Hot.xml file:
Copy Code code as follows:

<?xml version= "1.0" encoding= "gb2312"?>
<root>
<item>
<name> Yifei </name>
<url>MingXing/LiuYiFei.htm</url>
<color>red</color>
</item>
<item>
<name> Jolin Tsai </name>
<url>MingXing/CaiYiLin.htm</url>
<color>blue</color>
</item>
<item>
<name> Zhangnala </name>
<url>MingXing/ZhangNaLa.htm</url>
<color>green</color>
</item>
<item>
<name> Angela Culvert </name>
<url>MingXiang/ZhangShaoHan.htm</url>
<color>grey</color>
</item>
<item>
<name> Jane Zhang </name>
<url>MingXing/ZhangLiangYin.htm</url>
<color>black</color>
</item>
<item>
<name> shrieked Spring </name>
<url>MingXing/LiYuChun.htm</url>
<color>yellow</color>
</item>
<item>
<name> Vivian Hsu </name>
<url>MingXing/XuLuXuan.htm</url>
<color>pink</color>
</item>
</root>

demo1.html file:

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>javascript Get XML Data </title>
<script language= "JavaScript" >
Copy Code code as follows:

var xmldoc=new activexobject ("Microsoft.XMLDOM"); Creating XMLDOM Objects
Xmldoc.async=true; Using asynchronous loading
Xmldoc.onreadystatechange=loadedsales;
function Loadedsales ()
{
var txt= "";
if (xmldoc.readystate = = 0) {
Alert ("0");
}
if (xmldoc.readystate = = 1) {
Alert ("1");
}
if (xmldoc.readystate = = 2) {
Alert ("2");
}
if (xmldoc.readystate = = 3) {
Alert ("3");
}
if (xmldoc.readystate = 4)
{
if (xmlDoc.parseError.errorCode!= 0)
{
txt= XML Parse Error! ";
}else{
var items=xmldoc.documentelement.selectnodes ("item");
if (items!= null && items.length > 0)
{
for (Var i=0 i < items.length; i++)
{
TXT + + "<li><a href=" +items[i].childnodes[1].text+ "mce_href=" +items[i].childnodes[1].text+ "style=" color: "Mce_style=" color: "+items[i].childnodes[2].text+" > "+items[i].childnodes[0].text+" </a></li> ";
}
}else{
Txt= "";
}
}
}else{
Txt= "";
}
document.getElementById ("Sales"). Innerhtml=txt;
}
function Loadxmldoc ()
{
var url= "Hot.xml";
Xmldoc.load (URL);
}
</script>

<body onload= "Loadxmldoc ()" >
<div id= "Sales" ></div>
</body>

Effect Chart:
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.