Solved

pattern matching help

Posted on 2003-11-29
6
306 Views
Last Modified: 2010-03-04
i need help parseing an xml type:

$item =~
<allInfo>
    <item>
       ITEM A
    </item>
    <item>
       ITEM B
    </item>
</allInfo>
~;

i want to create an array of all the items @items of ITEM A and ITEM B.

0
Comment
Question by:mattaustin
  • 2
  • 2
  • 2
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 9844327
use XML::Parser;
$item = q~
<allInfo>
    <item>
       ITEM A
    </item>
    <item>
       ITEM B
    </item>
</allInfo>
~;
our $ic;
our @items;
sub ch{
}
sub ic{
    my ($p, $el) = @_;
    $ic .= $el;
}
sub sh{
    my ($p, $el, %atts) = @_;
    $p->setHandlers('Char' => \&ic) if ($el eq 'item');
}
sub eh{
    my ($p, $el) = @_;
    if ($el eq 'item' ){
        push @items,$ic;
        $ic = '';
        $p->setHandlers('Char' => \&ch)
    }
;
}
$p = new XML::Parser(Handlers => {Start => \&sh,
                                  End   => \&eh,
                                  Char  => \&ch,
                                  });
$p->parse($item);
print "@items";
0
 
LVL 3

Accepted Solution

by:
terageek earned 100 total points
ID: 9851716
A simpler (although not as elegent) way,

@items = $item =~ /<item>\s*(.*?)\s*<\/item>/gm;
0
 
LVL 84

Expert Comment

by:ozo
ID: 9854833
you mean /s, not /m
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 3

Expert Comment

by:terageek
ID: 9854931
You're right.  I meant /gs not /gm.
0
 
LVL 2

Author Comment

by:mattaustin
ID: 9855334
sub parse{
my @ret;
($string, $tag) = @_;
       while($string =~ m/<$tag>(.*?)<\/$tag>/sg){
       push(@ret,$1);
       }
return @ret;
}
0
 
LVL 2

Author Comment

by:mattaustin
ID: 9855339
thats what i ended up useing, and that ans seemed to be the closest thanks all.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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