Extract XML attribute using perl

Posted on 2007-10-09
Last Modified: 2010-05-19
#!/usr/bin/perl -w
use strict;
my $i;

open (IN, "a.svg") or die;
while (<IN>) {
    if (/<member name=\"(.+?)\" post=\"(.+?)\" location/) {
        print "$1\n";
<Member name="ABC" post = "EFG" location = "QWE"></Member>

The above perl script I had written print out the value of name & post, but what if I only want to print out the valuse of location, how do I do it???
Question by:xiaoxin85
    LVL 19

    Expert Comment

    Just add another group, $1, $2, $3 refer to the match inside the brackets

    open (IN, "a.svg") or die;
    while (<IN>) {
        if (/<member name=\"(.+?)\" post=\"(.+?)\" location=\"(.+?)\" /) {
            print "name: $1 post: $2 location: $3\n";

    Author Comment

    I want to onli get that particular attribute location because sometime the member element attribute might not be in this order. What if the "location" is the 1st attribute instead of 3rd? If is like that, then ur code do not work...
    LVL 39

    Accepted Solution

    There are modules that will handle all of the parsing of an XML file for you.   The easiest is XML::Simple.
    To do what you want using that moduel:

    use XML::Simple;

    #Read entire file into memory
    my $xml=XMLin("a.svg");

    #Get location
    my $location = $xml->{Member}->{location};

    If that doesn't get location, what is the output from this:
    use Data::Dumper;
    print Dumper($xml);

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity XML Read all nodes and 4 49
    wefewf 2 28
    for loop with Set 4 38
    Parsing the XML data to SQL Server 4 39
    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now