简体   繁体   中英

Is there a way of iterating through a specific XML tag in Ruby?

Is it possible to iterate over a specific XML tag in Ruby? In my case I want iterate over the desc tag in the following XML code:

<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>

So far, here is the code I use:

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

# iterate over desc here

I want to iterate over each desc tags so that I get the following output:

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

Any suggestions?

I'd also recommend using the Nokogiri gem. Something like this ought to work:

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

Nokogiri example that includes the name attribute for the who node:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM