Re: "Beginner" question on parsing XML file with XML::Simple
Grant McLean <[email protected]>
| Newsgroups | gmane.comp.lang.perl.xml |
|---|---|
| Message-ID | <[email protected]> |
Hi Johan
I'm the author of the XML::Simple module and frankly I don't think it's
the best module for the job. Although I was able to come up with a
solution it was very likely to fail in the face of slight variations in
the XML.
A better bet would be XML::LibXML. Here's a solution that's not only
shorter than the XML::Simple version, but more robust as well:
#!/usr/bin/perl -w
use strict;
use XML::LibXML;
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file('report.xml');
my $msg_path = q{/ReportOutput/ReportBody/ReportSection[@name='messages']}
. q{/ReportSection[@category='message']};
foreach my $message ($doc->findnodes($msg_path)) {
my $user = $message->findvalue(q{./String[@name='user']});
print "User: $user\n";
my $text = $message->findvalue(q{./String[@name='message']});
$text =~ s/\s+/ /g;
print "Text: $text\n";
}
You might also be interested in the following article I wrote on the
PerlMonks web site which introduces XML::LibXML ...
http://www.perlmonks.org/index.pl?node_id=490846
Cheers
Grant
On Sun, 2008-12-14 at 23:14 +0100, Johan Landerholm wrote:
> Hi all,
>
> I understand there is a lot of very talented people on this mailing list.
>
> I would consider myself a bad programmer even though I'm been in the
> Unix industry for 20 years.
> 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 ?
>
> Am I using the correct module ?
> Has anyone any suggestions on how to do this ?
>
> Best regards,
> Johan
>
>
> _______________________________________________
> Perl-XML mailing list
> [email protected]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs