使用 Java Script 从 URL 解析 XML/RSS

2022-01-10 00:00:00 xml-parsing javascript

您好,我想从 http://rss.news 之类的实时网址解析 xml/rss.yahoo.com/rss/entertainment 使用纯 Java 脚本(不是 jquery).我用谷歌搜索了很多.没有什么对我有用.任何人都可以帮助处理一段工作代码.

Hi i want to parse xml/rss from a live url like http://rss.news.yahoo.com/rss/entertainment using pure Java Script(not jquery). I have googled a lot. Nothing worked for me. can any one help with a working piece of code.

推荐答案

(你不能用谷歌搜索很多.)一旦你有 解决了同源策略,如果资源使用 XML MIME 类型(在这种情况下,text/xml),您可以执行以下操作:

(You cannot have googled a lot.) Once you have worked around the Same Origin Policy, and if the resource is served with an XML MIME type (which it is in this case, text/xml), you can do the following:

var x = new XMLHttpRequest();
x.open("GET", "http://feed.example/", true);
x.onreadystatechange = function () {
  if (x.readyState == 4 && x.status == 200)
  {
    var doc = x.responseXML;
    // …
  }
};
x.send(null);

(另请参阅 AJAX 和 XMLHttpRequest Level 2 其他事件处理程序属性的规范 [工作草案].)

(See also AJAX, and the XMLHttpRequest Level 2 specification [Working Draft] for other event-handler properties.)

本质上:无需解析.如果您想访问 XML 数据,请使用标准 DOM Level 2+ 核心 或 DOM Level 3 XPath 方法,例如

In essence: No parsing necessary. If you then want to access the XML data, use the standard DOM Level 2+ Core or DOM Level 3 XPath methods, e.g.

/* DOM Level 2 Core */
var title = doc.getElementsByTagName("channel")[0].getElementsByTagName("title")[0].firstChild.nodeValue;

/* DOM Level 3 Core */
var title = doc.getElementsByTagName("channel")[0].getElementsByTagName("title")[0].textContent;

/* DOM Level 3 XPath (not using namespaces) */
var title = doc.evaluate('//channel/title/text()', doc, null, 0, null).iterateNext();

/* DOM Level 3 XPath (using namespaces) */
var namespaceResolver = (function () {
  var prefixMap = {
    media: "http://search.yahoo.com/mrss/",
    ynews: "http://news.yahoo.com/rss/"
  };

  return function (prefix) {
    return prefixMap[prefix] || null;
  };
}());

var url = doc.evaluate('//media:content/@url', doc, namespaceResolver, 0, null).iterateNext();

(为了方便,另请参见 JSX:xpath.js,不使用 jQuery 的命名空间感知 DOM 3 XPath 包装器.)

(See also JSX:xpath.js for a convenient, namespace-aware DOM 3 XPath wrapper that does not use jQuery.)

但是,如果由于某些(错误)原因 MIME 类型不是 XML MIME 类型,或者如果 DOM 实现无法识别它,您可以使用最近浏览器中内置的解析器之一来解析 responseText 属性值.有关适用于 IE/MSXML 的解决方案,请参阅 pradeek 的答案.以下应该适用于其他任何地方:

However, if for some (wrong) reason the MIME type is not an XML MIME type, or if it is not recognized by the DOM implementation as such, you can use one of the parsers built into recent browsers to parse the responseText property value. See pradeek's answer for a solution that works in IE/MSXML. The following should work everywhere else:

var parser = new DOMParser();
var doc = parser.parseFromString(x.responseText, "text/xml");

如上所述进行.

在运行时使用功能测试来确定给定实现的正确代码分支.最简单的方法是:

Use feature tests at runtime to determine the correct code branch for a given implementation. The simplest way is:

if (typeof DOMParser != "undefined")
{
  var parser = new DOMParser();
  // …
}
else if (typeof ActiveXObject != "undefined")
{
  var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  // …
}

另请参阅 DOMParser 和 HTML5:DOM 解析和序列化(工作草案).

相关文章