使用js实现html加载xml内容(本地电脑使用)

使用js实现html加载xml内容(本地电脑使用)
xml中就是网址和名称,在html中只出现名称,点击跳转到一个网址。
求,完整的xml和html文件。

JS中有原生函数,支持解析xml字符串或者文件。

(new DOMParser()).parseFromString(xxxxx)

这个代码可以解析XML字符串,为对象。

<html>
<body>
<script type="text/javascript">
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, Chrome, etc.
    {
    xmlDoc=document.implementation.createDocument("","",null);
    }
  catch(e) {alert(e.message)}
  }
try 
  {
  xmlDoc.async=false;
  xmlDoc.load("books.xml");
  document.write("xmlDoc is loaded, ready for use");
  }
catch(e) {alert(e.message)}
</script>
</body>
</html>

上面这段代码可以解析一个XML文件。


具体的可参考下面教程,或者小乐阅读(Chrome浏览器中的RSS阅读器)中的源代码


http://www.w3school.com.cn/xmldom/dom_parser.asp

温馨提示:答案为网友推荐,仅供参考