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

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



Title: XML::Twig, get_xpath are cool

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