最简单的dom解析xml

<html>
<head>
<script language="JavaScript" for="window">
var xmlDoc;
    function 
loadXml(){    if (window.ActiveXObject) {
// code for IE
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("myfile.xml");
getmessage();
} else if (
document.implementation && document.implementation.createDocument) {
// code for Mozilla, etc.
xmlDocdocument.implementation.createDocument("","",null);
xmlDoc.load("myfile.xml");
xmlDoc.onload=getmessage
} else {
alert('Your browser cannot handle this script');
}
}


     function 
getmessage(){
    
document.getElementById("title").innerHTML=xmlDoc.getElementsByTagName("title")[0].firstChild.nodeValue
    document
.getElementById("author").innerHTML=xmlDoc.getElementsByTagName("author")[0].firstChild.nodeValue
    document
.getElementById("email").innerHTML=xmlDoc.getElementsByTagName("email")[0].firstChild.nodeValue
    document
.getElementById("date").innerHTML=xmlDoc.getElementsByTagName("date")[0].firstChild.nodeValue
    
}

</script>
<title>在HTML中调用XML数据</title>
</head>
<body bgcolor="#FFFFFF" onload="loadXml()"> 
<b>标题: </b> 
   <span id="title"></span><br> 
<b>作者: </b> 
   <span id="author"></span><br>
<b>电子信箱: </b> 
   <span id="email"></span><br> 
<b>日期: </b>
   <span id="date"></span><br>  
</body>
</html>

该xml文件如下:

<?xml version="1.0" encoding="gb2312"?>
<myfile>
<title>xml实用进阶</title>
<author>魅力奇异岭</author>
<email>zhyt710@tom.com</email>
<date>20060423</date></myfile>

Tags: dom, xml

« 上一篇 | 下一篇 »

信息推荐

发表评论