admin管理员组文章数量:1323744
I want to parse this XML Document ,%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
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
Share Improve this question edited Nov 27, 2008 at 15:10 Thomaschaaf asked Nov 27, 2008 at 14:59 ThomaschaafThomaschaaf 18.2k32 gold badges99 silver badges130 bronze badges6 Answers
Reset to default 3Unless 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.
There is a good tutorial at Ajaxian on how to parse XML with jQuery http://ajaxian./archives/ajaxian-featured-tutorial-parsing-xml-with-jquery hope it helps
edit: the tutorial actually is here: http://blog.reindel./2007/09/24/jquery-and-xml-revisited/
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.
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.
I created a jQuery plugin that parses XML pretty easily. It works in all Yahoo A grade browsers and es with filtering, limit and callback options.
It might be a solution to consider: http://jparse.kylerush/
I think I found a tutorial (its in german though) http://www.aboutwebdesign.de/awd/content/1124893836.shtml
本文标签: How to parse XML in JavaScript from GoogleStack Overflow
版权声明:本文标题:How to parse XML in JavaScript from Google - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742118193a2421568.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论