We help IT Professionals succeed at work.

Extract XML attribute using perl

xiaoxin85
xiaoxin85 asked
on
1,505 Views
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/) {
        $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???
Comment
Watch Question

Kim RyanIT Consultant

Commented:
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";
    }
}

Author

Commented:
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...
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.