2

I am learning XML parsing with RUBY(infact never did any XML parsing before). Plz refer this link - http://www.tutorialspoint.com/ruby/ruby_xml_xslt.htm In the DOM-like Parsing example, it prints all Movie Title followed by Movie Type. But i want to print Title and type as a pair, and then move to next xml node.

How can i achieve this?

jefflunt
  • 33,527
  • 7
  • 88
  • 126
rajya vardhan
  • 1,121
  • 4
  • 16
  • 29
  • 1
    What do you mean that you want to print `title and type as a pair`? Do you mean that you don't want `type` to be an element under `title`, but instead to have the `type` attribute at the same level of the XML heirarchy? – jefflunt Apr 02 '12 at 13:42

1 Answers1

2

You can ask the movie for any of its elements by name by using an array-like syntax:

# This will output all the movie titles and types                                                                                                                                                                                                                                
xmldoc.elements.each("collection/movie"){|e|
   puts "Movie Title : " + e.attributes["title"]
   puts e.elements["type"].text
}
joelparkerhenderson
  • 34,808
  • 19
  • 98
  • 119