有没有办法在Ruby中迭代特定的XML标签?

时间:2015-01-26 22:18:18

标签: ruby-on-rails ruby xml nokogiri rexml

是否可以迭代Ruby中的特定XML标记?在我的情况下,我想迭代以下XML代码中的desc标记:

<desc>
     <id>2408</id>
     <who name="Joe Silva">joe@silva.com</who>
     <when>Today</when>
     <thetext>Hello World</thetext>
</desc>
<desc>
     <id>2409</id>
     <who name="Joe Silva2">joe2@silva.com</who>
     <when>Future</when>
     <thetext>Hello World Again</thetext>
</desc>

到目前为止,这是我使用的代码:

xml_doc = agent.get("www.somewhere.com/file.xml")
document = REXML::Document.new(xml_doc.body);

# iterate over desc here

我想迭代每个desc标记,以便获得以下输出:

commentid : 2408
name : Joe Silva
who : joe@silva.com
bug_when : Today
thetext : Hello World 

commentid : 2409
name : Joe Silva2
who : joe2@silva.com
bug_when : Future
thetext : Hello World Again

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

我还建议使用Nokogiri gem。这样的事情应该有效:

require 'open-uri'
require 'nokogiri'

# fetch and parse the document
doc = Nokogiri::HTML(open('www.somewhere.com/file.xml'))

# search with css selectors
puts doc.at('desc id').text

# search by xpath
puts doc.at_xpath('//desc/id').text

# to iterate over a specific tag
doc.css('desc').each do |tag|
  puts tag.css('id').text
  # ...
end

答案 1 :(得分:2)

包含name节点的who属性的Nokogiri示例:

require 'nokogiri'

doc = Nokogiri.XML '
<root>
  <desc>
     <id>2408</id>
     <who name="Joe Silva">joe@silva.com</who>
     <when>Today</when>
     <thetext>Hello World</thetext>
  </desc>
  <desc>
    <id>2409</id>
     <who name="Joe Silva2">joe2@silva.com</who>
     <when>Future</when>
     <thetext>Hello World Again</thetext>
  </desc>
</root>
'

doc.css("desc").each do |desc|
  puts "commentid : #{desc.css("id").text}"
  puts "name : #{desc.css("who").attribute("name")}"  
  puts "who : #{desc.css("who").text}"
  puts "bug_when : #{desc.css("when").text}"
  puts "the text : #{desc.css("thetext").text}"  
end