服务器之家:专注于服务器技术及软件下载分享
分类导航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服务器之家 - 编程语言 - JavaScript - 用javascript读取xml文件读取节点数据

用javascript读取xml文件读取节点数据

2021-02-07 22:13JavaScript教程网 JavaScript

这篇文章主要介绍了用javascript读取xml文件读取节点数据的具体实现,需要的朋友可以参考下

下面代码是读取的节点数据,还有一种情况是读取节点属性数据。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<head>
  <title></title>
  <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>
</head>
  <body>
    <input id="Button1" type="button" value="read" onclick="ajaxrequst()" />
    <div id="newsInfo"></div>
  </body>

延伸 · 阅读

精彩推荐