将Nokogiri文档转换为Ruby哈希表。

75

有没有一种简单的方法将 Nokogiri XML 文档转换为哈希表?

类似于 Rails 的 Hash.from_xml 方法。


1
实际上,Rails的Hash.from_xml被整洁地包装在Rails代码的MiniXML部分中。自从我写下它以来,我一直想提取它。如果你很快没有听到关于它的消息,请给我一个提示。 - Joseph Holsten
我发布了一份修改过的 Ashan Ali 代码版本,它适用于属性并使用 Nokogiri - dimus
Hash.from_xml(nokogiri_doc.to_xml) 是否存在问题? - JellicleCat
我发现Ox比Nokogiri快5倍,因此这里有一个使用Ox的示例 - https://gist.github.com/amolpujari/5966431,在其中搜索任何元素并以哈希形式获取它。 - Amol Pujari
@JellicleCat,是的。不要浪费CPU使用Nokogiri解析XML,只是为了让Nokogiri将其输出为XML,以便由其他东西解析。只需传递原始XML即可完成。 - the Tin Man
8个回答

108
如果您想将Nokogiri XML文档转换为哈希值,只需执行以下操作:
require 'active_support/core_ext/hash/conversions'
hash = Hash.from_xml(nokogiri_document.to_s)

1
请解释一下 from_xml 是从哪里来的。它不是标准的 Ruby 方法。 - the Tin Man
4
from_xml 来自 ActiveSupport 模块。 - ScottJShea
1
它来自于这里:http://api.rubyonrails.org/classes/Hash.html#method-c-from_xml,代码是:`typecast_xml_value(unrename_keys(ActiveSupport::XmlMini.parse(xml)))`。 - Dorian
1
这应该是最简洁的答案,给这位作者点个赞 - Alexis Rabago Carvajal
9
注意:提问者知道 from_xml 并提到需要类似的功能。使用 from_xml 并不能回答问题。另外,如果文档已经是 Nokogiri 文档,则不要将其转换为字符串,然后再使用其他 XML 解析器进行解析。相反,传递原始 XML,并忽略使用 Nokogiri 进行解析。其他做法都是浪费 CPU 时间。 - the Tin Man
补充@theTinMan的评论。使用Nokogiri解析xml,然后将其转换为字符串,再将其转换为哈希表是不必要的。如果使用active_support,可以直接使用Hash::from_xml。例如:Hash.from_xml(File.read('some.xml'))就可以工作了。 - mbigras

22

这里有一个更简单的版本,可以创建一个包含命名空间信息的强大哈希表,适用于元素和属性:

require 'nokogiri'
class Nokogiri::XML::Node
  TYPENAMES = {1=>'element',2=>'attribute',3=>'text',4=>'cdata',8=>'comment'}
  def to_hash
    {kind:TYPENAMES[node_type],name:name}.tap do |h|
      h.merge! nshref:namespace.href, nsprefix:namespace.prefix if namespace
      h.merge! text:text
      h.merge! attr:attribute_nodes.map(&:to_hash) if element?
      h.merge! kids:children.map(&:to_hash) if element?
    end
  end
end
class Nokogiri::XML::Document
  def to_hash; root.to_hash; end
end

实际应用中的样例:

xml = '<r a="b" xmlns:z="foo"><z:a>Hello <b z:m="n" x="y">World</b>!</z:a></r>'
doc = Nokogiri::XML(xml)
p doc.to_hash
#=> {
#=>   :kind=>"element",
#=>   :name=>"r",
#=>   :text=>"Hello World!",
#=>   :attr=>[
#=>     {
#=>       :kind=>"attribute",
#=>       :name=>"a", 
#=>       :text=>"b"
#=>     }
#=>   ], 
#=>   :kids=>[
#=>     {
#=>       :kind=>"element", 
#=>       :name=>"a", 
#=>       :nshref=>"foo", 
#=>       :nsprefix=>"z", 
#=>       :text=>"Hello World!", 
#=>       :attr=>[], 
#=>       :kids=>[
#=>         {
#=>           :kind=>"text", 
#=>           :name=>"text", 
#=>           :text=>"Hello "
#=>         },
#=>         {
#=>           :kind=>"element", 
#=>           :name=>"b", 
#=>           :text=>"World", 
#=>           :attr=>[
#=>             {
#=>               :kind=>"attribute", 
#=>               :name=>"m", 
#=>               :nshref=>"foo", 
#=>               :nsprefix=>"z", 
#=>               :text=>"n"
#=>             },
#=>             {
#=>               :kind=>"attribute", 
#=>               :name=>"x", 
#=>               :text=>"y"
#=>             }
#=>           ], 
#=>           :kids=>[
#=>             {
#=>               :kind=>"text", 
#=>               :name=>"text", 
#=>               :text=>"World"
#=>             }
#=>           ]
#=>         },
#=>         {
#=>           :kind=>"text", 
#=>           :name=>"text", 
#=>           :text=>"!"
#=>         }
#=>       ]
#=>     }
#=>   ]
#=> }

1
那太棒了! - Steffen Roller

14

我在尝试将XML转换为哈希表(非Rails环境)时发现了这个东西。我本来想使用Nokogiri,但最终选择了Nori

然后我的代码变得简单明了:

response_hash = Nori.parse(response)

其他用户指出这个方法无效。我没有验证,但似乎解析方法已经从类移动到实例。我的上述代码曾经有效。新的(未经验证的)代码如下:

response_hash = Nori.new.parse(response)

我认为这是非使用Rails框架的应用程序的最佳解决方案。 - B Seven
未经验证的代码行可以工作。但是,如果您有一个 Nokogiri::XML 文档,则必须首先调用其 to_s 方法。例如:xml = Nokogiri::XML(File.open('file.xml')) 然后 hash = Nori.new.parse(xml.to_s),但是字段似乎以 Array 的形式返回,没有字段名称。 - code_dredd
在试图使用Nokogiri时,我一直在苦苦挣扎,最终找到了这个。这绝对是最好的解决方案!感谢您的帖子。 - Albert Rannetsperger
我喜欢它的输出属性是以“@”为前缀的。 - konsolebox

14

我在使用libxml-ruby(1.1.3)的代码。我个人没有使用过nokogiri,但我知道它无论如何都使用libxml-ruby。我还鼓励你看看ROXML(http://github.com/Empact/roxml/tree),它将xml元素映射到ruby对象;它是建立在libxml之上的。

# USAGE: Hash.from_libxml(YOUR_XML_STRING)
require 'xml/libxml'
# adapted from 
# http://movesonrails.com/articles/2008/02/25/libxml-for-active-resource-2-0

class Hash 
  class << self
        def from_libxml(xml, strict=true) 
          begin
            XML.default_load_external_dtd = false
            XML.default_pedantic_parser = strict
            result = XML::Parser.string(xml).parse 
            return { result.root.name.to_s => xml_node_to_hash(result.root)} 
          rescue Exception => e
            # raise your custom exception here
          end
        end 

        def xml_node_to_hash(node) 
          # If we are at the root of the document, start the hash 
          if node.element? 
           if node.children? 
              result_hash = {} 

              node.each_child do |child| 
                result = xml_node_to_hash(child) 

                if child.name == "text"
                  if !child.next? and !child.prev?
                    return result
                  end
                elsif result_hash[child.name.to_sym]
                    if result_hash[child.name.to_sym].is_a?(Object::Array)
                      result_hash[child.name.to_sym] << result
                    else
                      result_hash[child.name.to_sym] = [result_hash[child.name.to_sym]] << result
                    end
                  else 
                    result_hash[child.name.to_sym] = result
                  end
                end

              return result_hash 
            else 
              return nil 
           end 
           else 
            return node.content.to_s 
          end 
        end          
    end
end

太棒了!我只需要将 = strict 改为 = false。谢谢! - Ivan
啊...非常抱歉,我正在处理的文件没有任何属性(遗留的XML!)。 - A.Ali
9
Nokogiri不使用libxml-ruby,而是使用C库libxml2。 - skrat

14

使用 Nokogiri 解析 XML 响应到 Ruby 哈希表中。它非常快。

doc = Nokogiri::XML(response_body) 
Hash.from_xml(doc.to_s)

13
doc.to_s 返回的是 response_body 中已经有的内容,因此在你的示例中使用 nokogiri 是没有用的。 - Alesya Huzik
1
@alesguzik基本上是正确的,因为您在该语句中解析了XML两次。Hash.from_xml默认使用REXML而不是Nokogiri,也不确定您是否可以更改此设置。 - Jesse Whitham
2
Nokogiri有时比解析格式不良或编码错误的XML更具弹性。我有一些例子,其中Hash.from_xml(xml_str)会失败,但这仍然有效。因此,它可以作为Hash.from_xml(xml_str)的备选方案。 - user4887419
请注意,如果准确性很重要,则不应使用Hash.from_xml函数。该函数在处理更复杂的XML文档时会出现问题,完全忽略某些值。 - pyRabbit

4
如果您在配置文件中定义如下内容:
ActiveSupport::XmlMini.backend = 'Nokogiri'

这包括 Nokogiri 中的一个模块,您可以使用 to_hash 方法。


0

看看我为Nokogiri XML节点制作的简单混入。

http://github.com/kuroir/Nokogiri-to-Hash

这是一个使用示例:
require 'rubygems'
require 'nokogiri'
require 'nokogiri_to_hash'
html = '
  <div id="hello" class="container">
    <p>Hello! visit my site <a href="http://kuroir.com">Kuroir.com</a></p>
  </div>
'
p Nokogiri.HTML(html).to_hash
=> [{:div=>{:class=>["container"], :children=>[{:p=>{:children=>[{:a=>{:href=>["http://kuroir.com"], :children=>[]}}]}}], :id=>["hello"]}}]

0
如果您在Nokogiri中选择的节点只包含一个标签,您可以将键、值提取出来并将它们压缩成一个哈希表,如下所示:
  @doc ||= Nokogiri::XML(File.read("myxmldoc.xml"))
  @node = @doc.at('#uniqueID') # this works if this selects only one node
  nodeHash = Hash[*@node.keys().zip(@node.values()).flatten]

查看http://www.ruby-forum.com/topic/125944以获取有关Ruby数组合并的更多信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接