Link to home
Start Free TrialLog in
Avatar of xiaoxin85
xiaoxin85

asked on

Extract XML attribute using perl

#!/usr/bin/perl -w
use strict;
my $i;

open (IN, "a.svg") or die;
while (<IN>) {
    if (/<member name=\"(.+?)\" post=\"(.+?)\" location/) {
        $i++;
        print "$1\n";
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
a.svg
<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???
Avatar of Kim Ryan
Kim Ryan
Flag of Australia image

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=\"(.+?)\" /) {
        $i++;
        print "name: $1 post: $2 location: $3\n";
    }
}
Avatar of xiaoxin85
xiaoxin85

ASKER

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...
ASKER CERTIFIED SOLUTION
Avatar of Adam314
Adam314

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial