[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] Beginning working with XML::Twig.



2011/6/6 Dan Linder <dan@linder.org>:
> Anyone want to throw me some additional subroutines I need to grok?

If this is something that you want or need to do with XML::Twig, then feel free to ignore the rest of this e-mail.  If you're just trying to extract the data from the XML, and you don't care how, then another option to consider is XML::XPath.  I've read lots of good things about XML::Twig, and heard great things about it from people who have used it, but I have yet to need it.  However, I have had cases where I needed to extract some data from an XML file, and the best tool I've found for simple extraction is XML::XPath.  Below is some sample code that pulls out the two bits of data you specified using XML::XPath.

#!/usr/bin/env perl
# vim: ts=3 sw=3 et sm ai smd sc bg=dark
#######################################################################
# xpath-test.pl - example script to show extracting data with XML::XPath
#######################################################################

use 5.012; # Enable modern features.
use utf8;

use strict;
use warnings;
use Carp;
use XML::XPath;
use XML::XPath::XMLParser;

# Read in our XML File.
my $xp = XML::XPath->new(filename => 'test-data.xml');

# Try to get the start time - Note that we're not getting back a real
# string here, we're getting back an XML::XPath::Literal with available
# stringification
my $start_time = $xp->findvalue('/collection_status/check_starttime');
say "Start Time: " . $start_time;
croak "Couldn't get start time" unless $start_time;

# Now we'll try to pull back our instance times
my $nodeset = $xp->find('/collection_status/instance/time_of_last_rate_dp');

if (my @nodelist = $nodeset->get_nodelist) {
  # If we got here, then there were (valid) results from our find
  foreach my $node (@nodelist) {
     say "Instance Time: " . $node->string_value;
  }
}
else {
  croak "Couldn't find any last rate times";
}


> Dan

--
Christopher