当前位置: 代码迷 >> JavaScript >> JavaScript获取XML资料的数据
  详细解决方案

JavaScript获取XML资料的数据

热度:102   发布时间:2012-11-01 11:11:32.0
JavaScript获取XML文件的数据

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript获取RSS订阅XML文件的数据</title>
<link rel="shortcut icon" href="http://www.zhangxinxu.com/zxx_ico.png" />
<link rel="stylesheet" href="http://www.zhangxinxu.com/study/css/common.css?ver=20091122" type="text/css" />
<style type="text/css">
.zxx_xml_list{margin:30px; padding-left:25px;}
.zxx_xml_list li{list-style-type:disc; padding:3px 0;}
.zxx_xml_list li span{font-size:10px; color:#666666; margin-left:5px;}
</style>
</head>

<body>

<div class="zxx_out_box">
??? <div class="zxx_in_box">
????????
??????? <h1 class="zxx_title">JavaScript获取RSS订阅XML文件的数据</h1>

??????? <div class="zxx_main_con">
???<ul class="zxx_xml_list" id="xmlBox">
??????????? ?
??????????? </ul>
???<script type="text/javascript">
????var html = "";
????var xml;
????//var url = "http://www.infzm.com/rss/home/rss2.0.xml";
????var url = "../xml/rss2.0.xml";
????if(window.XMLHttpRequest){
?????var xhr = new XMLHttpRequest();
?????xhr.open("GET",url,false);
?????xhr.send(null);
?????xml = xhr.responseXML.documentElement;
????}else if(window.ActiveXObject){
?????xml = new ActiveXObject("Msxml2.DOMDocument");
?????xml.async = false;
?????xml.load(url);
????}else{
?????xml = "";
?????alert("没有获取到xml数据");
????}
????//显示数据
????var items = xml.getElementsByTagName("item");
????if(items.length > 10){
?????for(var i=0; i<10; i+=1){
??????html += '<li><a href="'+items.item(i).getElementsByTagName("link")[0].childNodes[0].nodeValue+'">'+items.item(i).getElementsByTagName("title")[0].childNodes[0].nodeValue+'</a><span>'+items.item(i).getElementsByTagName("pubDate")[0].childNodes[0].nodeValue+'</span></li>';?
?????}
????}else{
?????for(var i=0; i<items.length; i+=1){
??????html += '<li><a href="'+items.item(i).getElementsByTagName("link")[0].childNodes[0].nodeValue+'">'+items.item(i).getElementsByTagName("title")[0].childNodes[0].nodeValue+'</a><span>'+items.item(i).getElementsByTagName("pubDate")[0].childNodes[0].nodeValue+'</span></li>';?
?????}
????}
????document.getElementById("xmlBox").innerHTML = html;?
???</script>
??????? </div>
?????
??? </div>
</div>


</body>
</html>

  相关解决方案