简体   繁体   English

如何在Rails中获取XML页面并将其解析为Ruby哈希?

[英]How to I GET an XML page and parse it into a Ruby hash in Rails?

Assuming my xml response is at example.com/query , how do I parse it into a Ruby hash without using external dependencies like Nokogiri? 假设我的xml响应位于example.com/query ,如何不使用Nokogiri这样的外部依赖项将其解析为Ruby哈希呢?

Also, and not really part of the question, why would anyone use XML? 另外,不是所有人真正要考虑的问题,为什么有人会使用XML?

This is trivial: 这很简单:

require 'open-uri'

url = 'example.com/query'
h = Hash.from_xml(open url)

This example needs Hash#from_xml from Rail's active_support/core_ext/hash and will not work with Vanilla Ruby. 此示例需要Rail的active_support/core_ext/hash Hash#from_xml ,不适用于Vanilla Ruby。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM