REXML basic question

Hi. I’ve just started to play with REXML …

 $ cat x.xml
 <?xml version="1.0"?>
 <doc title="FooBar", directory="foobar">
   <section name="S1">
     <title>Foo</title>
      <body>text goes here </body>
   </section>

   <section name="S2">
     <title>Bar</title>
       <body>text goes here </body>
   </section>
 </doc>

How do I iterate over the sections? I have:

     @doc = Document.new File.new( "x.xml" )
     @doc.elements.each("tutorial/doc") { |elem|
       section = elem.attributes["name"]
     }

Gives me “S1” and “S2”. How do I iterate over these
sections to get and ?

I am missing something simple…

-mark.

Mark Probert wrote:

How do I iterate over the sections? I have:

    @doc = Document.new File.new( "x.xml" )
    @doc.elements.each("tutorial/doc") { |elem|
      section = elem.attributes["name"]
    }

Gives me “S1” and “S2”. How do I iterate over these
sections to get and ?

Something like this?

 doc.elements.each("doc") do |doc_element|
   doc_element.elements.each("section") do |section_element|
     section_element.elements.each("title") do |title_element|
       puts title_element.text
     end
     section_element.elements.each("body") do |body_element|
       puts body_element.text
     end
   end
 end

Hope this helps,

Lyle

Lyle Johnson wrote in comp.lang.ruby:

How do I iterate over the sections? I have:

Something like this?
…excellent code removed…

Thanks, Lyle.
Perfect.

Regards,
-mark.