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???
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???
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
open (IN, "a.svg") or die;
while (<IN>) {
if (/<member name=\"(.+?)\" post=\"(.+?)\" location=\"(.+?)\" /) {
$i++;
print "name: $1 post: $2 location: $3\n";
}
}