使用Java Script从URL解析XML / RSS [英] Parsing XML / RSS from URL using Java Script

查看:179
本文介绍了使用Java Script从URL解析XML / RSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

(你不能用Google搜索。)一旦你有解决同源策略,如果资源是用 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+ Core 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 是一个方便的,名称空间感知的DOM 3 XPath包装器,它不使用jQuery。)

(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解析和序列化(工作草案)

这篇关于使用Java Script从URL解析XML / RSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆