[Omaha.pm] Grab all the Error and Warnings out of XML, s/^\n//mg

Jay Hannah jhannah at omnihotels.com
Mon Sep 10 10:43:21 PDT 2007


Wrote this this morning as a quick proof of some XML tag capture I needed. I
like the s/^\n//mg trick, which gets rid of the annoying blank lines in
$in_message scalar.

j



my $in_message = <<EOT;
<?xml version="1.0" encoding="utf-8"?>

<OTA_HotelResModifyNotifRQ
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation="http://www.opentravel.org/OTA/2003/05
file:///C:/liaison/Omni 1.00/FS_OTA_HotelResModifyNotifRQ.xsd"
EchoToken="L00024547MR" TimeStamp="2007-09-10T08:19:06.986850-05:00"
Version="1.003" ResStatus="Modify"
xmlns="http://www.opentravel.org/OTA/2003/05">

  <POS>

    <Source PseudoCityCode="AUSCTR" ISOCountry="US" />

  </POS>

  <HotelResModifies>

    <HotelResModify CreateDateTime="2007-09-10T08:19:06.986850-05:00"
ResStatus="In-house">
-snip!-
EOT

my $out_message = <<EOT;
<OTA_HotelResModifyNotifRS EchoToken="L00024547MR" ResResponseType="Ignored"
TimeStamp="2007-09-10T08:27:58.53-05:00">
  <Errors>
    <Error Language="en-us" Type="3" ShortText="Invalid arrival time"
Code="294">
    </Error>
  </Errors>
</OTA_HotelResModifyNotifRS>
EOT

my @bad_stuff;
foreach ($in_message, $out_message) {
   my $twig = new XML::Twig;
   $twig->parse($_);
   my $root = $twig->root;
   push @bad_stuff, map { $_->sprint } $root->get_xpath('//Error');
   push @bad_stuff, map { $_->sprint } $root->get_xpath('//Warning');
}

print join "\n", @bad_stuff;
print "\n";
$in_message =~ s/^\n//mg;
print "$in_message\n";


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/omaha-pm/attachments/20070910/16d228eb/attachment.html 


More information about the Omaha-pm mailing list