有没有一种简单的方法将 Nokogiri XML 文档转换为哈希表?
类似于 Rails 的 Hash.from_xml
方法。
有没有一种简单的方法将 Nokogiri XML 文档转换为哈希表?
类似于 Rails 的 Hash.from_xml
方法。
require 'active_support/core_ext/hash/conversions'
hash = Hash.from_xml(nokogiri_document.to_s)
from_xml
是从哪里来的。它不是标准的 Ruby 方法。 - the Tin Manfrom_xml
并提到需要类似的功能。使用 from_xml
并不能回答问题。另外,如果文档已经是 Nokogiri 文档,则不要将其转换为字符串,然后再使用其他 XML 解析器进行解析。相反,传递原始 XML,并忽略使用 Nokogiri 进行解析。其他做法都是浪费 CPU 时间。 - the Tin Manactive_support
,可以直接使用Hash::from_xml
。例如:Hash.from_xml(File.read('some.xml'))
就可以工作了。 - mbigras这里有一个更简单的版本,可以创建一个包含命名空间信息的强大哈希表,适用于元素和属性:
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=>"!"
#=> }
#=> ]
#=> }
#=> ]
#=> }
我在尝试将XML转换为哈希表(非Rails环境)时发现了这个东西。我本来想使用Nokogiri,但最终选择了Nori。
然后我的代码变得简单明了:
response_hash = Nori.parse(response)
其他用户指出这个方法无效。我没有验证,但似乎解析方法已经从类移动到实例。我的上述代码曾经有效。新的(未经验证的)代码如下:
response_hash = Nori.new.parse(response)
Nokogiri::XML
文档,则必须首先调用其 to_s
方法。例如:xml = Nokogiri::XML(File.open('file.xml'))
然后 hash = Nori.new.parse(xml.to_s)
,但是字段似乎以 Array
的形式返回,没有字段名称。 - code_dredd我在使用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使用 Nokogiri 解析 XML 响应到 Ruby 哈希表中。它非常快。
doc = Nokogiri::XML(response_body)
Hash.from_xml(doc.to_s)
doc.to_s
返回的是 response_body
中已经有的内容,因此在你的示例中使用 nokogiri 是没有用的。 - Alesya HuzikHash.from_xml
函数。该函数在处理更复杂的XML文档时会出现问题,完全忽略某些值。 - pyRabbitActiveSupport::XmlMini.backend = 'Nokogiri'
这包括 Nokogiri 中的一个模块,您可以使用 to_hash
方法。
看看我为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"]}}]
@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数组合并的更多信息。
Hash.from_xml(nokogiri_doc.to_xml)
是否存在问题? - JellicleCat