Posted by: Bob Ngu on: April 1, 2007
My RoR efforts have culminated in JiggyMe, check it out!
This took me a while to find out but if you ever want to walk the tree from any entry point, use the each_recursive method. An example code snippet to do so ….
The REXML documentation does not make it easy to figure this out. The reason the method is available is because Element inherits from Parent which inherits from Child. In child.rb, there is a “require\node” statement. node.rb is where the each_recursive method is defined. And yes in case you haven’t noticed, the way to get an a tag element’s name is via the name() method.
I got the example above from the following article . There are other examples duplicated here for your convenience.
If you just want the next level of children, but no deeper, I’m not
sure what you call. I did this when I played with REXML, and the
obvious each_child doesn’t give you an REXML::Element. It gives a
REXML::Text element at the first iteration, then the next
REXML::Element, then another REXML::Text object, etc. Not quite what
you want. But adding this to your code will work.
It probably exists in some form in the REXML module, but I can’t
find it, so I recreated it (by a little hacking of the modules
each_recurse).
You can then
or recursively walk the tree by calling each_child_element for each
returned childElement (as with the first example)
January 15, 2009 at 2:30 am
I want to add a word in my xml document which is stored in my rails app’s public directory. How can i do that?