在 Rails 中打开并解压缩 XML URL
我正在构建一个 Rails 应用程序,该应用程序从托管在第 3 方服务器上的 XML 数据源获取有关产品的信息。该 XML 是通过 gzip 压缩发送的,我在使用它时遇到了很大的困难。
我在 Google 上花了相当多的时间来解决这个问题,但我的搜索结果似乎更多是关于发送 Gzipped 输出而不是接收 Gzipped 输入。
我找到的解决方案来自 StackOverflow,但我仍然遇到错误。
我首先要做的是将 XML 数据打印到浏览器,然后我就可以开始对其进行处理。这是我当前的代码:
<前><代码> def load_data url =“http://xml.domain.com/datafeed/” xml_input = Net::HTTP.get(URI.parse(url)) zstream = Zlib::Inflate.new @xml_output = zstream.inflate(xml_input) zstream.完成 zstream.关闭 结尾
我从中得到的错误是:
Zlib::DataError in Cron/get datafeedController#load_data
标头检查不正确
我想这意味着数据不是预期的格式,但我无法在任何地方找到有关如何正确执行此操作的信息。我排除了两件事:URL 有效且响应已压缩,但我不知道如何克服这一点。
任何帮助将不胜感激:-)
I'm building a rails app that takes information about products from an XML datafeed hosted on a 3rd party server. This XML is sent gzipped, and I'm having serious difficulty in getting anywhere with it.
I've spent a fair bit of time with Google on this, but the results of my searching seem to be more about Sending Gzipped output rather than receiving a Gzipped input.
The closed I've come to a solution came from StackOverflow, but I'm still getting errors.
What I'm trying to do in the first instance is print the XML data to the browser, then I can start with the processing of it. Here's my current code:
def load_data url = "http://xml.domain.com/datafeed/" xml_input = Net::HTTP.get(URI.parse(url)) zstream = Zlib::Inflate.new @xml_output = zstream.inflate(xml_input) zstream.finish zstream.close end
The error I'm getting from it is:
Zlib::DataError in Cron/get datafeedController#load_data
incorrect header check
I guess this means that the data isn't in the format that is expected, but I can't find information about how to do this properly anywhere. Two things I've ruled out is that the URL is valid and the response is Gzipped, but I'm stuck with how to get past this.
Any help would be greatly appreciated :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已排序!
然后加载到 Hpricot 中进行 XML 解析:
Sorted!
Then to load into Hpricot to do the XML parsing: