如何在 Google 的 JavaScript 中解析 XML

发布于 2024-07-09 08:43:36 字数 317 浏览 4 评论 0原文

我想解析此 XML 文档 http://www.google.de/ ig/api?weather=Braunschweig,%20Deutschland 我希望能够读出条件、temp_c 和湿度。 所有这些我想在 JavaScript 内部完成,而不使用任何服务器端脚本(例如 PHP),并且我希望它能够在现代浏览器以及 IE7 上工作,如果没有太多问题 IE6 编辑: 没有框架的解决方案将是理想的

I want to parse this XML Document http://www.google.de/ig/api?weather=Braunschweig,%20Deutschland I want to be able to read out condition, temp_c and humidity. All this I want to do inside of JavaScript without using any server sided scripts such as PHP and I want it to work on modern browsers as well as IE7 and if without many problems IE6
EDIT:
A solution without a framework would be ideal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

策马西风 2024-07-16 08:43:36

除非您正在处理 google.de 上托管的页面,否则您不能。

同源策略可防止在您网站的网页中运行的 JS 访问远程文档地点。

您必须涉及服务器端进程才能将 XML 转换为可以远程处理的格式(例如 JSON-P,其中 Yahoo! Pipes 将为您做)或通过您自己的服务器中继数据,并使用与页面同源可访问的服务器端进程。

Unless you are working on a page hosted on google.de — you can't.

The same origin policy prevents JS running in a webpage on your site from accessing a document on a remote site.

You have to involve a server side process in order to either convert the XML into a format that you can handle remotely (such as JSON-P, which Yahoo! Pipes will do for you) or relay the data through your own server with a server side process accessible in the same origin as the page.

痴者 2024-07-16 08:43:36

Ajaxian 有一个很好的教程,介绍如何使用 jQuery 解析 XML
http://ajaxian.com/archives/ajaxian-featured-教程-用-jquery 解析 xml
希望它有助于

编辑:教程实际上在这里: http: //blog.reindel.com/2007/09/24/jquery-and-xml-revisited/

There is a good tutorial at Ajaxian on how to parse XML with jQuery
http://ajaxian.com/archives/ajaxian-featured-tutorial-parsing-xml-with-jquery
hope it helps

edit: the tutorial actually is here: http://blog.reindel.com/2007/09/24/jquery-and-xml-revisited/

终遇你 2024-07-16 08:43:36

我在 javascript 中解析 XML 时遇到了一些运气,代码如下:

var xmlDoc = this.req.responseXML.documentElement;
var tStatus = xmlDoc.getElementsByTagName("status")[0].firstChild.data;
var tOtherURL = xmlDoc.getElementsByTagName("otherurl")[0].firstChild.data;
var tRows = xmlDoc.getElementsByTagName("rows")[0].firstChild.data;

请记住,我并不真正了解 Javascript,并且我从其他地方对此进行了货物崇拜。

I've had some luck parsing XML in javascript with code like:

var xmlDoc = this.req.responseXML.documentElement;
var tStatus = xmlDoc.getElementsByTagName("status")[0].firstChild.data;
var tOtherURL = xmlDoc.getElementsByTagName("otherurl")[0].firstChild.data;
var tRows = xmlDoc.getElementsByTagName("rows")[0].firstChild.data;

Keep in mind that I don't really know Javascript and I cargo-cult'ed this from somewhere else.

明月夜 2024-07-16 08:43:36

Firefox 和 Opera 中,解决方案非常简单:它们具有内置的 XPath 支持。 因此,您只需评估 XML 响应的 DOM 上的 XPath 表达式。

对于 IE,还有更多工作要做。 其他人基本上问过同样的问题之前和合理的答案是:您需要使用允许您使用 XPath 的 JavaScript 库。 否则,您最终会自己编写针对特定问题的解析代码。

In Firefox and Opera the solution is very easy: They have builtin XPath support. So you just evaluate an XPath expression on the DOM of the XML response.

With IE there's more work to do. Someone else asked basically the same question before and the reasonable answer is: You'll need to employ JavaScript library that will allow you to use XPath. Otherwise you end up writing your problem-specific parsing code yourself.

So尛奶瓶 2024-07-16 08:43:36

我创建了一个 jQuery 插件,可以非常轻松地解析 XML。 它适用于所有雅虎 A 级浏览器,并带有过滤、限制和回调选项。

这可能是一个值得考虑的解决方案: http://jparse.kylerush.net/

I created a jQuery plugin that parses XML pretty easily. It works in all Yahoo A grade browsers and comes with filtering, limit and callback options.

It might be a solution to consider: http://jparse.kylerush.net/

苏佲洛 2024-07-16 08:43:36

我想我找到了一个教程(虽然是德语)http://www.aboutwebdesign。 de/awd/content/1124893836.shtml

I think I found a tutorial (its in german though) http://www.aboutwebdesign.de/awd/content/1124893836.shtml

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文