在 Rails 中打开并解压缩 XML URL

发布于 2024-08-31 06:18:10 字数 802 浏览 1 评论 0原文

我正在构建一个 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 技术交流群。

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

发布评论

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

评论(1

怂人 2024-09-07 06:18:10

已排序!

file = Net::HTTP.get(URI.parse(url))
gz = Zlib::GzipReader.new(StringIO.new(file))
whole_xml = gz.read

然后加载到 Hpricot 中进行 XML 解析:

hp = Hpricot(whole_xml)

Sorted!

file = Net::HTTP.get(URI.parse(url))
gz = Zlib::GzipReader.new(StringIO.new(file))
whole_xml = gz.read

Then to load into Hpricot to do the XML parsing:

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