[Omaha.pm] XML::Twig, get_xpath are cool

Jay Hannah jhannah at omnihotels.com
Wed Jul 18 13:33:48 PDT 2007


The fact that you can hop up to your parent element with ../ in xpath is just way too cool.  :)

j



$ cat j.pl
use XML::Twig;
my $xml = grab_xml();
my $twig = XML::Twig->new();
$twig->parse($xml);
my $root = $twig->root;
foreach my $i ($root->get_xpath('Inventories/Inventory/StatusApplicationControl')) {
   print $i->{att}->{InvType};
   foreach my $j ($i->get_xpath('../InvCounts/InvCount')) {
      print " " . $j->{att}->{Count};
   }
   print "\n";
}

sub grab_xml {
   return <<EOT;
<OTA_HotelInvCountNotifRS>
  <Inventories>
    <Inventory>
      <StatusApplicationControl InvType="RODG" Start="2007-07-28" End="2007-07-28">
      </StatusApplicationControl>
      <InvCounts>
        <InvCount Count="-2">
        </InvCount>
      </InvCounts>
    </Inventory>
    <Inventory>
      <StatusApplicationControl InvType="KNSUP" Start="2007-07-28" End="2007-07-28">
      </StatusApplicationControl>
      <InvCounts>
        <InvCount Count="-95">
        </InvCount>
      </InvCounts>
    </Inventory>
    <Inventory>
      <StatusApplicationControl InvType="THOR" Start="2007-07-28" End="2007-07-28">
      </StatusApplicationControl>
      <InvCounts>
        <InvCount Count="1000">
        </InvCount>
      </InvCounts>
    </Inventory>
  </Inventories>
  <Success></Success>
</OTA_HotelInvCountNotifRS>
EOT
}

$ perl j.pl
RODG -2
KNSUP -95
THOR 1000
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/omaha-pm/attachments/20070718/dcbde884/attachment.html 


More information about the Omaha-pm mailing list