JavaScript captures XML data with sample screenshots
- 2020-03-30 02:12:11
- OfStack
Hot. XML file:
Not. The HTML file:
< HTML XMLNS = "http://www.w3.org/1999/xhtml" >
< Head>
< Meta HTTP - equiv = "content-type" Content = "text/HTML. Charset = gb2312 "/ >
< Title> JavaScript gets XML data < / title>
< Script language = "javascript" >
Effect:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201403/201403051050001.gif? 201425105019 ">
<?xml version="1.0" encoding="gb2312"?>
<root>
<item>
<name> l </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> Jang na-ra </name>
<url>MingXing/ZhangNaLa.htm</url>
<color>green</color>
</item>
<item>
<name> Angela </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> Li yuchun </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>
Not. The HTML file:
< HTML XMLNS = "http://www.w3.org/1999/xhtml" >
< Head>
< Meta HTTP - equiv = "content-type" Content = "text/HTML. Charset = gb2312 "/ >
< Title> JavaScript gets XML data < / title>
< Script language = "javascript" >
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); //Create the XmlDom object
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>
</head>
<body onLoad="loadXmlDoc()">
<div id="sales"></div>
</body>
</html>
Effect:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201403/201403051050001.gif? 201425105019 ">