Hi,

On 23-Nov-07, at 8:14 AM, Bulhac Mihai wrote:

> hello, im new in using rexml and a i have a first question: how can a
> read this file:
> <drive name="c">
>  <folder name="folder1">
>    <subfolder name="sub1">
>    </subfolder>
>    <subfolder name="sub2">
>        <file name="file1"></file>
>        <file name="file2"></file>
>    </subfolder>
>    <subfolder  name="sub3">
>        <file name="file1">
>    </subfolder>
>
>  </folder>
>  <folder name="folder2">
>      <file name="file1">
>      </file>
>      <file name="file2">
>      </file>
>  </folder>
> </drive>
>
> i want to read all children so that output will be something like  
> that:
> drive C
>  folder folder1
>       subfolder sub1
>       subfolder sub2
>            file file1
>            file file2
>       subfoler sub3
>   folder folder2
>        file file1
>        file file2
>
> How can i do that?, Tnx

You're going to have to work out which 'parser' you want to use. I  
think there are three in REXML: DOM, SAX, and a pull parser.

Choosing the parser to use is a little tricky, and will depend on what  
you plan to do with the parsed XML file.

FWIW, I tend to use either my own pull parser or libxml.

With my thing, xampl-pp (a pull parser), installed as a gem, you'd  
write this as:

require "xampl-pp"

def format(filename)
   xpp = Xampl_PP.new
   xpp.input = File.new(filename)

   loop do
     case xpp.nextEvent
        when Xampl_PP::START_ELEMENT
          print "  " * (xpp.depth - 1)
          print xpp.name
          xpp.attributeValue.each { | v | print " #{ v }" }
          print "\n"
        when Xampl_PP::END_DOCUMENT
         break
     end
   end
end

format("./test.xml")

And you'd get exactly the output you wanted.

This is such a simple use of the parser that it is hard to choose  
between them.

Cheers,
Bob


>
> -- 
> Posted via http://www.ruby-forum.com/.
>

----
Bob Hutchison                  -- tumblelog at http://www.recursive.ca/so/
Recursive Design Inc.          -- weblog at http://www.recursive.ca/hutch
http://www.recursive.ca/       -- works on http://www.raconteur.info/cms-for-static-content/home/