Rexml: how to get element type?

doc = REXML::Document.new <<EOF




EOF

doc.elements.each( “/a” ) do |a|
a.elements.each |child| do
# how to get child.type ? i.e. “b”, "c"
end
end

Thanks!

maybe you want .name() ?

···

il Wed, 21 Apr 2004 07:21:47 GMT, “Its Me” itsme213@hotmail.com ha scritto::

doc = REXML::Document.new <<EOF




EOF

doc.elements.each( “/a” ) do |a|
a.elements.each |child| do
# how to get child.type ? i.e. “b”, “c”
end
end

“gabriele renzi” surrender_it@remove.yahoo.it wrote in message

doc = REXML::Document.new <<EOF




EOF

doc.elements.each( “/a” ) do |a|
a.elements.each |child| do
# how to get child.type ? i.e. “b”, “c”
end
end

maybe you want .name() ?

Thanks, that worked.

name() does not show up in my RDoc (the version that came with my rexml
install)… Wonder why? name() is hardly likely to be a recent addition!

dunno, I found it looking at Element#inspect().
Btw could it be somehow possible to have a smaller string
repretsentation for rexml objects ? they screw up IRB work :wink:

···

il Wed, 21 Apr 2004 08:16:29 GMT, “Its Me” itsme213@hotmail.com ha scritto::

maybe you want .name() ?

Thanks, that worked.

name() does not show up in my RDoc (the version that came with my rexml
install)… Wonder why? name() is hardly likely to be a recent addition!

gabriele renzi surrender_it@remove.yahoo.it wrote in message news:hl2d80tenhogq1j97hgh3i3ue1t3p3caej@4ax.com

maybe you want .name() ?

Thanks, that worked.

name() does not show up in my RDoc (the version that came with my rexml
install)… Wonder why? name() is hardly likely to be a recent addition!

name() is a method that Element inherits from Namespace. I hate to
pass the buck, but this is a limitation in rdoc that it doesn’t
include methods inherited from other classes.

If you want the API documentation for name(), look in the Namespace
module. In fact, Element inherits from Parent and Child, and there
are methods in those classes that are not included in the
rdoc-generated API documentation for Element, either.

Btw could it be somehow possible to have a smaller string
repretsentation for rexml objects ? they screw up IRB work :wink:

Can you give me an example of what you’d like to see?

By the way, I find the following to be useful when looking for
methods; it is almost always more readable that #inspect :slight_smile:

obj.methods.sort

— SER

Can you give me an example of what you’d like to see?

hard to tell, I hoped you could answer with something like
“well I have this little routine…”
Btw, I believe that the main thing is not to let the representation to
recur over the whole document.
The representation for Document.new(“”).root
goes over then lines in irb (78 columns term), that makes it little
useful. It would be nice to avoid to show the child of the child,
replacing it with a simple ‘…’

Maybe it could be reasonable to avoid showing @parent, and avoiding
showing attributes like @prefix @namespace or @children or @atributes
when they are empty (‘’ or or {})

Notice that I’d like this cause I’m thinking in irb.
Maybe people make use of the actual result from inspect, and I’m plain
wrong.

By the way, I find the following to be useful when looking for
methods; it is almost always more readable that #inspect :slight_smile:

obj.methods.sort

I’ll add a puts on top of it to show them one per line :slight_smile:

···

il 24 Apr 2004 11:25:42 -0700, ser@germane-software.com (Sean Russell) ha scritto::

Btw could it be somehow possible to have a smaller string
repretsentation for rexml objects ? they screw up IRB work :wink:

Can you give me an example of what you’d like to see?

If I can jump in here… That last time I tried working with rexml, I
opened a medium sized xml file in irb. I created a new REXML::Document
IIRC, and it automatically inspected it, since it was the return value.
I literally took about five minutes for the text to scroll past on my
screen; When I called .inspect.size on the result, it was somewhere
around 1.5 megs. I kept working with it for a bit, adding a “;0” at the
end of each line so it wouldn’t inspect the return value.

for example, a stored settings file I wanted to work with:

(plist = open(“com.apple.internetconfig.plist”){|f|f.read}).size
=> 228291
doc = Document.new(plist); 0
=> 0
doc.inspect.size
=> 1424751

something useful would be a small list of attributes, the number of
children, etc… just as long as it’s no more than several lines of
text. :slight_smile: I credit my irb experience with rexml as the reason I still
don’t know how to use it.

By the way, I find the following to be useful when looking for
methods; it is almost always more readable that #inspect :slight_smile:

obj.methods.sort

I agree :slight_smile: Also, this can be useful if that shows way to much still:

 (obj.methods - Object.new.methods).sort

I added that to my .irbrc file defined as Object#less_methods.

cheers,
–Mark

···

On Apr 24, 2004, at 11:29 AM, Sean Russell wrote:

Mark Hubbart wrote:

(obj.methods - Object.new.methods).sort

I added that to my .irbrc file defined as Object#less_methods.

You can use Object.instance_methods instead, since

Object.instance_methods == Object.new.methods

Maybe this would be convenient:

class Object
def less_methods(cl=Object)
methods - cl.instance_methods
end
end

3.less_methods(Numeric)

=>[“%”, “<<”, “>>”, “prec_i”, “&”, “to_sym”, “*”, “+”, “times”, “-”,

“prec_f”, “/”, “upto”, “|”, “~”, “chr”, “to_i”, “succ”, “^”, “**”,
“to_f”, “prec”, “size”, “next”, “id2name”, “”, “downto”]

Mark Hubbart wrote:

something useful would be a small list of attributes, the number of
children, etc… just as long as it’s no more than several lines of
text. :slight_smile: I credit my irb experience with rexml as the reason I still
don’t know how to use it.

Something I’ve found useful before in writing inspect methods for
hierarchical (but not XML) data is to have a depth argument to inspect,
with a default of 0. The depth is passed on to sub-things, after
decrementing. A depth of nil means you want to see the whole tree. Since
the default is 0, using p and irb just give you the top-level stuff,
whatever that means. For XML, maybe this would be, as Mark suggests, the
attributes and number of children. When you want more depth, you can
request it.

gabriele renzi surrender_it@remove.yahoo.it wrote in message news:t94n8098s3aqfrte1t0q65nmrf7poc0pl5@4ax.com

Can you give me an example of what you’d like to see?

hard to tell, I hoped you could answer with something like
“well I have this little routine…”
Btw, I believe that the main thing is not to let the representation to
recur over the whole document.

Naw. I haven’t touched the #inspect method. What you see is what
Ruby generates by default. IMO, the #inspect method for XML objects
would probably be XML, which wouldn’t be very useful.

useful. It would be nice to avoid to show the child of the child,
replacing it with a simple ‘…’

It wouldn’t be an accurate representation if you couldn’t see the
children… even Array#inspect recurses.

— SER

Mark Hubbart discord@mac.com wrote in message news:874825E2-96EA-11D8-9038-000502FDD5CC@mac.com

If I can jump in here… That last time I tried working with rexml, I
opened a medium sized xml file in irb. I created a new REXML::Document
IIRC, and it automatically inspected it, since it was the return value.

Hm. I do admit that #inspect on REXML objects is rather useless,
unless you’re doing debugging. I used it quite a bit when I was first
developing REXML. I could truncate it and have it show something
like:

<elem attr='val'> ... </elem>

Sort of a truncated XML representation.

— SER

Mark Hubbart wrote:

(obj.methods - Object.new.methods).sort

I added that to my .irbrc file defined as Object#less_methods.

You can use Object.instance_methods instead, since

Object.instance_methods == Object.new.methods

Maybe this would be convenient:

class Object
def less_methods(cl=Object)
methods - cl.instance_methods
end
end

Great idea :slight_smile: a quick hack now has turned into something better:

class Object
def less_methods(klass = Object)
(methods - klass.instance_methods).sort
end
end
class Class
def less_methods(klass = Class)
(methods - klass.methods).sort
end
end
class Module
def less_methods
(methods - Module.methods).sort
end
end

cheers,
–Mark

···

On Apr 25, 2004, at 12:06 PM, Joel VanderWerf wrote:

3.less_methods(Numeric)

=>[“%”, “<<”, “>>”, “prec_i”, “&”, “to_sym”, “*”, “+”, “times”,

“-”, “prec_f”, “/”, “upto”, “|”, “~”, “chr”, “to_i”, “succ”, “^”,
“**”, “to_f”, “prec”, “size”, “next”, “id2name”, “”, “downto”]

That would be nice :slight_smile: Basically, anything that would make it possible
to use REXML normally in irb would be greatly appreciated.

If the full #inspect is necessary sometimes (I don’t know), then
perhaps there could be a verbosity level of some sort? So that it could
be set like this:

REXML.verbose_inspect = false

This would make it useful in irb. Looking over the output from a simple
bit of xml, it looks like much could be done just by hiding a few
instance variables and all grandchildren.

cheers,
–Mark

···

On Apr 28, 2004, at 5:19 AM, Sean Russell wrote:

Mark Hubbart discord@mac.com wrote in message
news:874825E2-96EA-11D8-9038-000502FDD5CC@mac.com

If I can jump in here… That last time I tried working with rexml, I
opened a medium sized xml file in irb. I created a new REXML::Document
IIRC, and it automatically inspected it, since it was the return
value.

Hm. I do admit that #inspect on REXML objects is rather useless,
unless you’re doing debugging. I used it quite a bit when I was first
developing REXML. I could truncate it and have it show something
like:

Sort of a truncated XML representation.

As a side note,
In some Inspector kind of tool of mine, I too filter out some methods:
# Filter out most common methods that almost all objects implement
objmethods = 123.public_methods()
objmethods.concat 123.protected_methods()
objmethods.concat 123.private_methods()
# Filter out Enumerable’s predicates, they block on IO objects
tmp = Enumerable.instance_methods().reject { |m| m.include? “?” }
objmethods.concat tmp
When user inspect an object, I display the object’s instance variables and
the methods. For predicates, I directly display the predicate’s value.
User can disable filtering (at his own risk regarding blocking predicates).

Yours,

Jean-Hugues

···

At 15:28 27/04/2004 +0900, you wrote:

On Apr 25, 2004, at 12:06 PM, Joel VanderWerf wrote:

Mark Hubbart wrote:

(obj.methods - Object.new.methods).sort

I added that to my .irbrc file defined as Object#less_methods.
You can use Object.instance_methods instead, since
Object.instance_methods == Object.new.methods
Maybe this would be convenient:
class Object
def less_methods(cl=Object)
methods - cl.instance_methods
end
end
Great idea :slight_smile: a quick hack now has turned into something better:
class Object
def less_methods(klass = Object)
(methods - klass.instance_methods).sort
end
end
class Class
def less_methods(klass = Class)
(methods - klass.methods).sort
end
end
class Module
def less_methods
(methods - Module.methods).sort
end
end
cheers,
–Mark