[prev in list] [next in list] [prev in thread] [next in thread]
List: perl-xml
Subject: Re: "Beginner" question on parsing XML file with XML::Simple
From: "Jenda Krynicky" <Jenda () Krynicky ! cz>
Date: 2008-12-15 9:50:05
Message-ID: 4946365D.24917.4402E17 () Jenda ! Krynicky ! cz
[Download RAW message or body]
From: Johan Landerholm <johan@landerholm.net>
> 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>
===== Jenda@Krynicky.cz === 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
Perl-XML@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic