rexml - how to get element name

G

Gerald Preissler

If I have an instance of REXML::Element, is there a convenient way to
get it's element name? I would have expected a get_name method for
Element or something like that, but according to the stdlib
documentation at http://www.ruby-doc.org/stdlib/ there is no such thing.

I found two ways that I do not like very much:

1. get the xpath to the element via Element.xpath and then extract the
actual name:

path = el.xpath
name = path.sub(/^\/(.+\/)*(\w*)(\[\d*\])?/ ,'\2')

I don't like this method since it feels wasteful.

2. Use instance_eval to get the value of Element::expanded_name:

irb(main):013:0> el.instance_eval('@expanded_name')
=> "import"

I don't like that either (I know I'm picky) since this means to use part
of the rexml implementation that is not part of the publicly documented
interface.

Is there an other way that I would like?

Thanks in advance for all advise and insights.

regards
Jerry
 
L

Logan Capaldo

--Apple-Mail-8--236711120
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed


If I have an instance of REXML::Element, is there a convenient way
to get it's element name? I would have expected a get_name method
for Element or something like that, but according to the stdlib
documentation at http://www.ruby-doc.org/stdlib/ there is no such
thing.

ruby style avoids extraneous get prefixes to the front of method
names. It's just name, not get_name


--Apple-Mail-8--236711120--
 
G

Gerald Preissler

Logan said:
ruby style avoids extraneous get prefixes to the front of method names.
It's just name, not get_name

Logan,

thanks to you and Nick for your replies.

Shouldn't that show up in the the docsas an attribute of Element?

regards
Jerry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top