foo = <root>
<m>This is <b>bold</b> and <i>
<b>italic<br/>text</b></i>.<br/></m>
</root>
doc = Nokogiri::XML foo
doc.xpath("//m").each {|a|
a.children.each{|v|
puts "#{v.name}::[#{v.text}]"
}
}
如何解析<m>
内所有标签中的所有文字?
我需要产生以下输出......
t="This is "
b="bold"
t=" and "
i=""
b="italic"
br="<br/>"
t="text"
...
等
https://gist.github.com/anonymous/e98fb4e4c254883a17a2
#!/usr/bin/env ruby
# encoding: utf-8
require 'nokogiri'
foo = <<-EOXML
<root>
<m>This is <b>bold</b> and <i><b>italic<br/>text</b></i>.<br/></m>
</root>
EOXML
def pr(v)
case v.class.to_s
when 'Nokogiri::XML::Text'
puts "[#{v.text}]"
when 'Nokogiri::XML::Element'
puts "'#{v.name.to_s}':: #{pr(v.children)}"
when 'Nokogiri::XML::NodeSet'
puts "#{v.children}"
else
puts "[-#{v.class.to_s}- #{v.to_s}]"
end
end
doc = Nokogiri::XML.parse foo
p foo
doc.xpath("//m").each {|a|
a.children.each{|v|
puts "#{v.name}::[#{v.text}]"
}
}
如何更改上面的代码以便它使用递归?