您的位置:首页 > Web前端 > JavaScript

js读取xml文件 (兼容IE和FF)

2013-07-26 15:36 411 查看
1

<script language="javascript" type="text/javascript">

2

var xmlDoc;

3

function showcurcity()

4

{

5

if(window.ActiveXObject)

6

{

7

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

8

xmlDoc.async="false"

9

xmlDoc.load("OlyCity.xml");

10

getvalue();

11

}

12

else if(document.implementation && document.implementation.createDocument)

13

{

14

xmlDoc=document.implementation.createDocument("", "root", null);

15

xmlDoc.load('OlyCity.xml');

16

xmlDoc.onload=getvalueff;

17


18

}

19

else

20

{

21

return null;

22

}

23

}

24

function getvalue()

25

{

26

var nodes=xmlDoc.documentElement.childNodes;

27

var now=new Date();

28

for(i=0;i<nodes.length;i++){

29

var value=nodes.item(i).childNodes.item(0).text ;

30

if(value==now.toLocaleDateString())

31

{

32

window.document.getElementById('curcity').innerText=nodes.item(i).childNodes.item(1).text;

33

window.document.getElementById('curcity').href =nodes.item(i).childNodes.item(2).text;

34

}

35

}

36


37

}

38


39

function getvalueff(){

40

var cCode=xmlDoc.getElementsByTagName("date");

41

var now = new Date();

42

var nowvalue=now.getFullYear()+'年'+(now.getMonth()+1)+'月'+now.getDate()+'日';

43

for(i=0;i<cCode.length;i++)

44

{

45


46

var value=cCode[i].childNodes[0].nodeValue;

47

if(value==nowvalue)

48

{

49

window.document.getElementById('curcity').href =xmlDoc.getElementsByTagName("url")[i].childNodes[0].nodeValue;;

50

window.document.getElementById('curcity').childNodes[0].nodeValue=xmlDoc.getElementsByTagName("city")[i].childNodes[0].nodeValue;

51

}

52

}

53

}

54


55

setInterval(showcurcity,1000);

56

</script>

以下是xml的片断.

1

<?xml version="1.0" encoding="gb2312" ?>

2

<root>

3

<citymessage>

4

<date>2008年4月29日</date>

5

<city>胡志明市</city>

6

<url>http://torchrelay.beijing2008.cn/cn/journey/hochiminhcity/</url>

7

</citymessage>

8

<citymessage>

9

<date>2008年4月30日</date>

10

<city>香港</city>

11

<url>http://torchrelay.beijing2008.cn/cn/journey/hongkong/</url>

12

</citymessage>

13

</root>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: