Solved

pattern matching help

Posted on 2003-11-29
6
323 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

751 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