Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

pattern matching help

Posted on 2003-11-29
6
Medium Priority
?
334 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 300 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

705 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