rexml - how to get element name

Discussion in 'Ruby' started by Gerald Preissler, Mar 26, 2006.

  1. 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
     
    Gerald Preissler, Mar 26, 2006
    #1
    1. Advertising

  2. Gerald Preissler

    Nick Sieger Guest

    ------=_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/rexml/rdoc/classes/REXML/Namespace.html

    element.name

    Cheers,
    /Nick

    ------=_Part_3282_29031284.1143426517684--
     
    Nick Sieger, Mar 27, 2006
    #2
    1. Advertising

  3. --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--
     
    Logan Capaldo, Mar 27, 2006
    #3
  4. 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
     
    Gerald Preissler, Mar 27, 2006
    #4
  5. 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/libdoc/rexml/rdoc/classes/REXML/
    Element.html

    -- Daniel
     
    Daniel Harple, Mar 27, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Damphyr
    Replies:
    2
    Views:
    167
    Damphyr
    Jul 16, 2003
  2. Daniel Berger

    rexml error - REXML::Validation

    Daniel Berger, Oct 12, 2004, in forum: Ruby
    Replies:
    2
    Views:
    171
    Henrik Horneber
    Oct 12, 2004
  3. Jesse P.
    Replies:
    0
    Views:
    189
    Jesse P.
    Jan 4, 2008
  4. Phlip
    Replies:
    0
    Views:
    166
    Phlip
    Jan 15, 2008
  5. Pierre Pat

    REXML get specific element

    Pierre Pat, Mar 18, 2009, in forum: Ruby
    Replies:
    14
    Views:
    210
    Phlip
    Mar 19, 2009
Loading...

Share This Page