in reply to Easiest way to parse a simple XML file?

My swiss-army-knife for XML is XML::Twig, which I used for this:

#!/usr/bin/perl use strict; use warnings; use XML::Twig; my $t = XML::Twig->new(); $t->parsefile('x.xml'); my @errors = map { my %d = map { (my $tag = $_->tag()) =~ s/^m://; $tag => $_->text() } $_->children(); \%d } $t->get_xpath('//m:error'); use Data::Dumper; print Dumper \@errors;
The output is:
$VAR1 = [ { 'col' => '66', 'message' => 'document type does not allow element "LINK" +here', 'line' => '11' }, { 'col' => '41', 'message' => 'document type does not allow element "LINK" +here', 'line' => '14' }, { 'col' => '4', 'message' => 'document type does not allow element "META" +here', 'line' => '21' } ];
You can do the same for warnings and whatever else you have. With some practice with the xpath, you can ensure that you're getting just what you want.

Replies are listed 'Best First'.
Re^2: Easiest way to parse a simple XML file?
by halfbaked (Sexton) on Dec 12, 2008 at 02:06 UTC
    Thanks Tanktalus, I went with XML::Twig as you suggested, it seems the easiest way to go, but I always kind of scratch my head whenever I have to parse XML, it never seems as simple as it should be.

    Maybe it's just me.

    Thanks again.