javascript 读取xml文件、读取节点数据的简单示例

前端之家收集整理的这篇文章主要介绍了javascript 读取xml文件、读取节点数据的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
读取的节点数据,还有一种情况是读取节点属性数据。 感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。经测试代码如下:

/**
 * 读取xml文件、读取节点数据
 *
 * @param 
 * @arrange (512.笔记) jb51.cc
 **/
<script type="text/javascript">
var objLength = null;
var xmlHttp;
var strurl = "";
function ajaxrequst() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
xmlHttp = new XMLHttpRequest();
}
try {
strurl = "/data/rdzz.xml";
xmlHttp.onreadystatechange = LoadXmlFile;
xmlHttp.open("GET",strurl + "?time=" + (new Date()).getTime(),true);
xmlHttp.send(null);
}
catch (e) {
}
}

function LoadXmlFile() {
var content = "";
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var xmlDOM = xmlHttp.responseXML;
var xmlRoot = xmlDOM.documentElement;
try {
var objLength = xmlRoot.getElementsByTagName("Item");

for (var i = 0; i < objLength.length; i++) {
var elementtitle = objLength[i].getElementsByTagName('NewsTitle')[0];
var elementtime = objLength[i].getElementsByTagName('NewsTime')[0];
var elementurl= objLength[i].getElementsByTagName('NewsUrl')[0];

// LastValue = objLength[i].getAttribute( "LastValue" )
// Prediction = objLength[i].getAttribute( "Prediction" )
// Actual = objLength[i].getAttribute( "Actual" )
// importance = objLength[i].getAttribute( "importance" )
// newstime = objLength[i].getAttribute( "NewsTime" )

var title = elementtitle.textContent || elementtitle.text;
var time = elementtime.textContent || elementtime.text;
var url = elementurl.textContent || elementurl.text;

content += "<li><dl><dd>" + time + "</dd><dd><a href='" + url + "' target='blank'>" + title + "</a></dd></dl></li>";
}
document.getElementById("newsInfo").innerHTML = content;
}
catch (e) {
}
} 
}
}
</script>

经测试代码如下:

<input id="Button1" type="button" value="read" onclick="ajaxrequst()" />
<div id="newsInfo"></div>

猜你在找的JavaScript相关文章