【问题标题】:Should I parse XML using JavaScript or use CSS to display it我应该使用 JavaScript 解析 XML 还是使用 CSS 来显示它
【发布时间】:2012-10-10 16:29:39
【问题描述】:

我正在构建一个 Winform 网络浏览器。我有一个存储浏览器历史记录的history.xml 文件。我需要在浏览器窗口中显示它,以便用户可以单击网站的超链接并进行导航。我想使用 JavaScript 来解析 XML 文件并以表格形式显示内容。如何使用 JS 解析 XML?我不确定在这种情况下使用什么。我已经有一个很好的带有 CSS 的 HTML 页面来显示历史记录。请指教。

我的 XML 文件如下所示。

<?xml version="1.0" encoding="utf-8"?>
<browsing>
  <history date="08/10/2012">
    <url>http://www.google.ca/</url>
    <time>12:52 AM</time>
  </history>
  <history date="08/10/2012">
    <url>http://www.facebook.com/</url>
    <time>12:53 AM</time>
  </history>
  <history date="08/10/2012">
    <url>http://ca.msn.com/</url>
    <time>9:51 PM</time>
  </history>
</browsing>

【问题讨论】:

  • 为什么不使用XSLT 设置样式并按原样显示?

标签: javascript jquery xml parsing


【解决方案1】:

使用 jQuery 的 $.get()。

$.get("history.xml", function(xml) {
  $(xml).find("history:nth(0)").find("url").val(); // returns http://www.google.ca/
  $(xml).find("history:nth(1)").getAttribute("date"); // returns 08/10/2012
}, "xml");

编辑:当我写这个答案时,你的帖子被编辑了。要以表格形式显示,使用XSLT 可能更容易。它专为设计 XML 样式而设计。

【讨论】:

    猜你喜欢
    • 2016-09-30
    • 2010-12-10
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多