Re: "Beginner" question on parsing XML file with XML::Simple
"Jenda Krynicky" <[email protected]>
| Newsgroups | gmane.comp.lang.perl.xml |
|---|---|
| Message-ID | <[email protected]> |
From: Johan Landerholm <[email protected]> > I have just come across a XML parsing problem that I thought perl would > be good in solving for me. > But, I have struggled with the XML::Simple module to get the information > from a xml report that I get from a system. > > If someone is willing to help me with this problem, I would be very > thankful! > > I have a report that I need to pull a few pieces of data from and do > something else with. > The report looks like this: > > > <?xml version="1.0" encoding="UTF-8"?> > <ReportOutput> > <ReportHead> > <Report oid="-1y2p0ij32e7uh:-1y2p0ij30wn8z" name="export log" > type="systemlog_rpt"> > <Description>Test report</Description> > </Report> > </ReportHead> > <ReportBody> > <ReportSection name="messages" category="messages"> > <ReportSection name="12/14/08 7:15 AM" category="message"> > <String name="user">administrator</String> > <Timestamp name="messageTime" displayvalue="12/14/08 > 7:15 AM">1229267714000</Timestamp> > <String name="message">Modified Rule 'test rule'. > > Added '/bin'. > </String> > <String name="level">Information</String> > <String name="category">Rule Change</String> > </ReportSection> > </ReportSection> > <ReportSection name="reportTotals" category="reportTotals"> > <Integer name="summary.messages">1</Integer> > <Integer name="summary.information">1</Integer> > <Integer name="summary.error">0</Integer> > </ReportSection> > </ReportBody> > </ReportOutput> > > The information that I need is the <String > name="user">*administrator*</String> and the <String > name="message">*Modified Rule 'test rule'. Added '/bin'.*</String>. > How is it possible to find this information in a perl script ? > I have tried to use the examples in the documentation but I can't get it > right ? You may use XML::Rules. And either transform the data into a datastructure that'll fit your needs or print the stuff you need as you go along: use strict; use XML::Rules; my $parser = XML::Rules->new( stripspaces => 7, start_rules => { ReportHead => 'skip', ReportSection => sub { if ($_[1]->{category} eq 'messages') { # nothing to write at the end of the messages ReportSection print "Writing messages from the report section '$_[1]- >{name}'\n\n"; } return 1; }, }, rules => { 'String,Integer' => sub {return '_'.$_[1]->{name} => $_[1]- >{_content}}, 'Timestamp' => sub {return '_'.$_[1]->{name} => $_[1]- >{displayvalue}}, 'ReportSection' => sub { if ($_[1]->{category} eq 'message') { print "The message at $_[1]->{name} written by $_[1]->{_user} at $_[1]->{_messageTime} was: $_[1]->{_message}\n\n"; } elsif ($_[1]->{category} eq 'messages') { # nothing to write at the end of the messages ReportSection } elsif ($_[1]->{category} eq 'reportTotals') { print "There were $_[1]->{'_summary.messages'} messages.\n"; } else { print "Unknown category '$_[1]->{category}'\n"; } return; }, }, ); $parser->parse(\*DATA); __DATA__ <?xml version="1.0" encoding="UTF-8"?> <ReportOutput> <ReportHead> <Report oid="-1y2p0ij32e7uh:-1y2p0ij30wn8z" name="export log" type="systemlog_rpt"> <Description>Test report</Description> </Report> </ReportHead> <ReportBody> <ReportSection name="messages" category="messages"> <ReportSection name="12/14/08 7:15 AM" category="message"> <String name="user">administrator</String> <Timestamp name="messageTime" displayvalue="12/14/08 7:15 AM">1229267714000</Timestamp> <String name="message">Modified Rule 'test rule'. Added '/bin'. </String> <String name="level">Information</String> <String name="category">Rule Change</String> </ReportSection> </ReportSection> <ReportSection name="reportTotals" category="reportTotals"> <Integer name="summary.messages">1</Integer> <Integer name="summary.information">1</Integer> <Integer name="summary.error">0</Integer> </ReportSection> </ReportBody> </ReportOutput> ===== [email protected] === http://Jenda.Krynicky.cz ===== When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery _______________________________________________ Perl-XML mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs