Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


get XML element name

get XML element name

get XML element name

Hi all,

I'm working with some XML files using perl-5.8.7 and p5-XML-XPath-1.13 on FreeBSD6.

How do I get the name of an XML element using XPath? I know the position of where this element would be. In the example below I need a function that returns the string "mystery_tag", knowing that this would be the first child of /root/data/details/


    <info>some text</info>

My knowledge of perl is quite limited so any links with examples using XPath would be appreciated.


RE: get XML element name

Have you considered any of the excellent XML parsers over at http://search.cpan.org.

Sorry dunno the 1st thing about XPath, but always go to cpan when I'm looking for something like that


Spend an hour a week on CPAN, helps cure all known programming ailments winky smile

RE: get XML element name

I'm with Paul, I don't know much about XPath - I did give it a shot with some results that could be helpful.  Did you look at the docs for XPath?  This is pretty much straight out of the documentation:


use XML::XPath;
use XML::XPath::XMLParser;

my $xp = XML::XPath->new(filename => 'test.txt');
my $nodeset = $xp->find('/root/data/details'); # find all nodes under details

foreach my $node ($nodeset->get_nodelist) {
    print "FOUND\n\n",
Which prints:



            <stuff />
So you could split the results of XML::XPath::XMLParser::as_string($node) into an array and look at the second element in that array to find the name of your mystery tag.

What are you doing with the XML info once it's parsed?  Is there a particular reason you're using XML::XPath as opposed to another parser?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close