Solved

pattern matching help

Posted on 2003-11-29
6
285 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
Comment Utility
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
Comment Utility
A simpler (although not as elegent) way,

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

Expert Comment

by:ozo
Comment Utility
you mean /s, not /m
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 3

Expert Comment

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

Author Comment

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

Author Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now