将Nokogiri文档转换为Ruby哈希

将Nokogiri文档转换为Ruby哈希

本文介绍了将Nokogiri文档转换为Ruby哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



类似于Rails的 Hash.from_xml

解决方案

我将这段代码与libxml-ruby(1.1.3)一起使用。我自己并没有使用过nokogiri,但我知道它使用libxml-ruby。我也鼓励你看看ROXML(),它将xml元素映射到ruby对象;它是在libxml之上构建的。

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

class哈希
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
#在此处引发您的自定义异常
结束
结束

def xml_node_to_hash(节点)
#如果我们位于文档的根目录,散列
if node.element?
如果node.children?
result_hash = {}

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

if child.name ==text
if!child.next?和!child.prev?
返回结果
结束
elsif result_hash [child.name.to_sym]
如果result_hash [child.name.to_sym] .is_a?(Object :: Array)
result_hash [child.name.to_sym]<<结果
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
返回零
结束
其他
返回node.content.to_s
结束$ b $结束
结束
结束


Is there an easy way to convert a Nokogiri XML document to a Hash?

Something like Rails' Hash.from_xml.

解决方案

I use this code with libxml-ruby (1.1.3). I have not used nokogiri myself, but I understand that it uses libxml-ruby anyway. I would also encourage you to look at ROXML (http://github.com/Empact/roxml/tree) which maps xml elements to ruby objects; it is built atop 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

这篇关于将Nokogiri文档转换为Ruby哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 11:50