用javascript读取xml文件读取节点数据
2014-08-15来源:易贤网

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

view sourceprint?

01 <head>

02 <title></title>

03 <script type="text/javascript">

04 var objLength = null;

05 var xmlHttp;

06 var strurl = "";

07 function ajaxrequst() {

08 if (window.ActiveXObject) {

09 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

10 }

11 else {

12 xmlHttp = new XMLHttpRequest();

13 }

14 try {

15 strurl = "/data/rdzz.xml";

16 xmlHttp.onreadystatechange = LoadXmlFile;

17 xmlHttp.open("GET", strurl + "?time=" + (new Date()).getTime(), true);

18 xmlHttp.send(null);

19 }

20 catch (e) {

21

22 }

23

24 }

25

26 function LoadXmlFile() {

27 var content = "";

28 if (xmlHttp.readyState == 4) {

29 if (xmlHttp.status == 200) {

30 var xmlDOM = xmlHttp.responseXML;

31 var xmlRoot = xmlDOM.documentElement;

32 try {

33 var objLength = xmlRoot.getElementsByTagName("Item");

34

35 for (var i = 0; i < objLength.length; i++) {

36 var elementtitle = objLength[i].getElementsByTagName('NewsTitle')[0];

37 var elementtime = objLength[i].getElementsByTagName('NewsTime')[0];

38 var elementurl= objLength[i].getElementsByTagName('NewsUrl')[0];

39

40 // LastValue = objLength[i].getAttribute( "LastValue" )

41 // Prediction = objLength[i].getAttribute( "Prediction" )

42 // Actual = objLength[i].getAttribute( "Actual" )

43 // importance = objLength[i].getAttribute( "importance" )

44 // newstime = objLength[i].getAttribute( "NewsTime" )

45

46 var title = elementtitle.textContent || elementtitle.text;

47 var time = elementtime.textContent || elementtime.text;

48 var url = elementurl.textContent || elementurl.text;

49

50 content += "<li><dl><dd>" + time + "</dd><dd><a href='" + url + "' target='blank'>" + title + "</a></dd></dl></li>";

51 }

52 document.getElementById("newsInfo").innerHTML = content;

53 }

54 catch (e) {

55 }

56 }

57 }

58 }

59 </script>

60 </head>

61 <body>

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

63 <div id="newsInfo"></div>

64 </body>

更多信息请查看IT技术专栏

推荐信息