zoukankan      html  css  js  c++  java
  • 我看IE与FFJs读取xml文件

    我几天为了实现一个小小的功能(奥运圣火传递路线).Js读取了下xml文件,为了适应IE与FF还是费了一点时间的.

     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>
    作者:孙会生
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    PAT (Advanced Level) 1010. Radix (25)
    PAT (Advanced Level) 1009. Product of Polynomials (25)
    PAT (Advanced Level) 1008. Elevator (20)
    PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)
    PAT (Advanced Level) 1006. Sign In and Sign Out (25)
    PAT (Advanced Level) 1005. Spell It Right (20)
    PAT (Advanced Level) 1004. Counting Leaves (30)
    PAT (Advanced Level) 1001. A+B Format (20)
    PAT (Advanced Level) 1002. A+B for Polynomials (25)
    PAT (Advanced Level) 1003. Emergency (25)
  • 原文地址:https://www.cnblogs.com/tunwa/p/1186714.html
Copyright © 2011-2022 走看看