Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: XML::parser question

by Shendal (Hermit)
on Oct 22, 2002 at 22:18 UTC ( #207214=note: print w/replies, xml ) Need Help??

in reply to XML::parser question

I find that XML::Simple is very well suited to this sort of task. The following may fit your needs:
#!/usr/bin/perl -w use strict; use XML::Simple; my $xml = << "EOF"; <?xml version="1.0" encoding="ISO-8859-1"?> <monk value="PM"> <say>JAPH</say> <vals> <val val1="F" val2="value f"> </val> <val val1="FO" val2="value fo"> </val> <val val1="FOO" val2="value foo"> </val> <val val1="FOOB" val2="value foob"> </val> <val val1="FOOBA" val2="value fooba"> </val> <val val1="FOOBAR" val2="value foobar"> </val> </vals> </monk> EOF my $xs = new XML::Simple; my $xmlhref = $xs->XMLin($xml); if ($xmlhref->{say}) { print $xmlhref->{say} . "\n" if (grep { $_->{val1} eq 'FOO' } @{$xm +lhref->{vals}->{val}}); }


Replies are listed 'Best First'.
Re: Re: XML::parser question
by signal9 (Pilgrim) on Oct 23, 2002 at 14:42 UTC

    I agree, XML::Simple is a nice module for quickly parsing simple XML data.

    Of course a question that may need to be answered is, "Why use a parser over a regexp?" Although the regular expression may do the job for you now, it may break in the future should the format of the incoming document change. No doubt you could extend your own routine to the point of being a full-featured parser, but that has already been done for you in the form of several very good parsing modules ( of course, if you can improve the world by writing a new parser, don't hesitate to do so! )

    Though the overhead of running a full-fledged parser may be a little greater than an ad hoc solution, it might save you some maintenance headaches later.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://207214]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2021-10-25 04:22 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (89 votes). Check out past polls.