使用 Java 脚本从 URL 解析 XML/RSS
您好,我想从实时网址解析 xml/rss,例如 http://rss.news.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(您不可能在 Google 上进行大量搜索。)解决同源政策后,如果资源使用 XML MIME 类型(其中 就是这种情况,
text/xml
),您可以执行以下操作:(参见还有 AJAX 和 XMLHttpRequest Level 2 规范[工作草案],用于其他事件处理程序属性。)
本质上:无需解析。如果您随后想要访问 XML 数据,使用标准 DOM Level 2+ Core 或 DOM Level 3 XPath 方法,例如
(另请参阅 JSX:xpath.js 用于不使用的方便的、命名空间感知的 DOM 3 XPath 包装器jQuery。)
但是,如果由于某种(错误)原因,MIME 类型不是 XML MIME 类型,或者 DOM 实现本身无法识别它,则可以使用最新浏览器中内置的解析器之一来解析 < code>responseText 属性值。请参阅 pradeek 的回答,了解适用于 IE/MSXML 的解决方案。以下内容应该适用于其他地方:
按照上述说明进行操作。
在运行时使用功能测试来确定给定实现的正确代码分支。最简单的方法是:
另请参阅
DOMParser
和 HTML5:DOM 解析和序列化(工作草案)。(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:(See also AJAX, and the XMLHttpRequest Level 2 specification [Working Draft] for other event-handler properties.)
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.
(See also JSX:xpath.js for a convenient, namespace-aware DOM 3 XPath wrapper that does not use jQuery.)
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:Proceed as described above.
Use feature tests at runtime to determine the correct code branch for a given implementation. The simplest way is:
See also
DOMParser
and HTML5: DOM Parsing and Serialization (Working Draft).您可能遇到的一个大问题是,通常无法跨域获取数据。对于大多数 RSS 提要来说,这是一个大问题。
处理javascript跨域加载数据的常用方式是调用JSONP。基本上,这意味着您正在检索的数据被包装在 JavaScript 回调函数中。您使用脚本标记加载 url,并在代码中定义该函数。因此,当加载脚本时,它会执行该函数并将数据作为参数传递给它。
大多数 xml/rss 提要的问题是仅提供 xml 的服务往往不提供 JSONP 包装功能。
在继续之前,请检查您的数据源是否提供 json 格式和 JSONP 功能。这将使事情变得容易得多。
现在,如果您的数据源不提供 json 和 jsonp 功能,您就必须发挥创意。
处理此问题的相对简单的方法是使用代理服务器。您的代理在您控制的某个地方运行,并充当中间人来获取您的数据。服务器加载您的 xml,然后您的 javascript 向它发出请求。如果代理服务器在相同的域名上运行,那么您可以只使用标准的 xhr(ajax) 请求,而不必担心跨域的问题。
或者,您的代理服务器可以将数据包装在 jsonp 回调中,您可以使用上面提到的方法。
如果您使用 jQuery,那么 xhr 和 jsonp 请求是内置方法,因此使编码变得非常容易。其他常见的js库应该也支持这些。如果您从头开始编写所有这些内容,则会需要更多工作,但并不是非常困难。
现在,一旦你获得了数据,希望它只是 json。那么就不需要解析了。
但是,如果您最终不得不坚持使用 xml/rss 版本,并且如果您是 jQuery,则可以简单地使用 jQuery.parseXML http://api.jquery.com/jQuery.parseXML/。
One big problem you might run into is that generally, you cannot get data cross domain. This is big issue with most rss feeds.
The common way to deal with loading data in javascript cross domain is calls JSONP. Basically, this means that the data you are retrieving is wrapped in a javascript callback function. You load the url with a script tag, and you define the function in your code. So when the script loads, it executes the function and passes the data to it as an argument.
The problem with most xml/rss feeds is that services that only provide xml tend not to provide JSONP wrapping capability.
Before you go any farther, check to see if your data source provides a json format and JSONP functionality. That will make this a lot easier.
Now, if your data source doesn't provide json and jsonp functionality, you have to get creative.
On relatively easy way to handle this is to use a proxy server. Your proxy runs somewhere under your control, and acts as a middleman to get your data. The server loads your xml, and then your javascript does the requests to it instead. If the proxy server runs on the same domain name then you can just use standard xhr(ajax) requests and you don't have to worry about cross-domain stuff.
Alternatively, your proxy server can wrap the data in a jsonp callback and you can use the method mentioned above.
If you are using jQuery, then xhr and jsonp requests are built-in methods and so make doing the coding very easy. Other common js libraries should also support these. If you are coding all of this from scratch, its a little more work but not terribly difficult.
Now, once you get your data hopefully its just json. Then there's no parsing needed.
However, if you end up having to stick with an xml/rss version, and if you're jQuery, you can simply use jQuery.parseXML http://api.jquery.com/jQuery.parseXML/.
最好将 xml 转换为 json。 http://jsontoxml.utilities-online.info/
转换后是否需要打印 json 对象检查本教程
http://www.w3schools.com/json/json_eval.asp
better convert xml to json. http://jsontoxml.utilities-online.info/
after converting if you need to print json object check this tutorial
http://www.w3schools.com/json/json_eval.asp