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

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



Ok, after finding that http://www.mirod.org/ is the only remaining XML::Twig documentation site (after http://search.cpan.org/dist/XML-Twig/Twig_pm.slow), I'm having trouble getting started.

(My XML file and some broken code is below.)

What I want to do is parse the XML file (around 300KB), and check the "time_of_last_rate_dp" section of each "instance" to compare with the check_starttime time in the beginning of the XML file.

I've got the simple code that can pull in the XML file, but I can't determine how to "walk" through the XML tree.  I thought I'd use the "findnodes()" or "children()" subroutines, but that's not quite it...

Anyone want to throw me some additional subroutines I need to grok?

Thanks,
Dan

=== begin code ===
#!perl -w
#
# Required modules:
# XML::Twig - perl -MCPAN -e "install XML::Twig"
#
use strict;
use XML::Twig;
my $filename = shift || die ("Must supply an XML file to parse.");
my $twig = XML::Twig->new(
pretty_print => 'indented'
);

$twig->parsefile($filename);
my $root = $twig->root;

# twig->print works fine and regurgitates the whole XML file...
$twig->print;

# This doesn't work...the "$instance" is a HASH.
foreach my $instance ($root->children('instance')) {
printf ("Found: $instance\n");
}
=== end code ===

Here is a portion of the XML file:
=== begin XML ===
<collection_status>
  <primary_server>10.10.10.11</primary_server>
  <check_interval time="720">720</check_interval>
  <check_starttime time="1307366104">06/06/11 09:15:04</check_starttime>
  <check_endtime time="1307366110">06/06/11 09:15:10</check_endtime>
  <instance>
    <motype_id>42815</motype_id>
    <motype_name>My MO Type name</motype_name>
    <inst_id>119863</inst_id>
    <device>
        <device_id>545</device_id>
        <device_name>My device name</device_name>
    </device>
    <agent>
        <agent_id>10248</agent_id>
        <agent_name>AABBCC (alpha)</agent_name>
        <agent_status>Active</agent_status>
    </agent>
    <name>Test+%28GOOGLE%29</name>
    <collect>TRUE</collect>
    <config>300</config>
    <time_of_last_rate_dp time="0">1969/12/31 16:00:00</time_of_last_rate_dp>
  </instance>
  <instance>
    <motype_id>28451</motype_id>
    <motype_name>HPUX FileSystem</motype_name>
    <inst_id>25251</inst_id>
    <device>
        <device_id>34</device_id>
        <device_name>NEHPX001</device_name>
    </device>
    <agent>
        <agent_id>10033</agent_id>
        <agent_name>NEHPX001</agent_name>
        <agent_status>Active</agent_status>
    </agent>
    <name>%2Foracle%2FEPP%2Foraarch</name>
    <collect>TRUE</collect>
    <config>300</config>
    <time_of_last_rate_dp time="0">1969/12/31 16:00:00</time_of_last_rate_dp>
  </instance>
</collection_status>
=== end XML ===

--
***************** ************* *********** ******* ***** *** **
"Quis custodiet ipsos custodes?"
    (Who can watch the watchmen?)
    -- from the Satires of Juvenal
"I do not fear computers, I fear the lack of them."
    -- Isaac Asimov (Author)
** *** ***** ******* *********** ************* *****************