Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > rexml - how to get element name

Reply
Thread Tools

rexml - how to get element name

 
 
Gerald Preissler
Guest
Posts: n/a
 
      03-26-2006
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
 
Reply With Quote
 
 
 
 
Nick Sieger
Guest
Posts: n/a
 
      03-27-2006
------=_Part_3282_29031284.1143426517684
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

On 3/26/06, Gerald Preissler <> wrote:
>
> If I have an instance of REXML::Element, is there a convenient way to
> get it's element name?



It's in the included REXML::Namespace module:

http://ruby-doc.org/stdlib/libdoc/re...Namespace.html

element.name

Cheers,
/Nick

------=_Part_3282_29031284.1143426517684--


 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      03-27-2006
--Apple-Mail-8--236711120
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed


On Mar 26, 2006, at 9:23 PM, Gerald Preissler wrote:

> 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--


 
Reply With Quote
 
Gerald Preissler
Guest
Posts: n/a
 
      03-27-2006
Logan Capaldo wrote:

>
> 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
 
Reply With Quote
 
Daniel Harple
Guest
Posts: n/a
 
      03-27-2006
On Mar 27, 2006, at 8:03 AM, Gerald Preissler wrote:

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


It's a downside of rdoc. Up at the top[1] you see that the Namespace
module is mixed in, but it's methods do not show up. ri actually
shows the mixed in methods -- I don't know why the HTML docs can't.

[1] http://www.ruby-doc.org/stdlib/libdo...classes/REXML/
Element.html

-- Daniel



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
REXML::Element.write is deprecated. See REXML::Formatters Phlip Ruby 0 01-15-2008 08:23 PM
REXML::Document could not parse UTF-8 "<name>\302</name>" Jesse P. Ruby 0 01-04-2008 05:37 PM
rexml error - REXML::Validation Daniel Berger Ruby 2 10-12-2004 04:19 PM
soap4r 1.4.8.1 with REXML 2.7.1 - no REXML::VERSION_MAJOR Damphyr Ruby 2 07-16-2003 09:49 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57