Re: XML::LibXML navigation
- From: pue@xxxxxxx (Andreas Puerzer)
- Date: Fri, 12 Jan 2007 21:25:47 +0100
Mumia W. schrieb:
On 01/12/2007 11:29 AM, Beginner wrote:
[snip]
Yes ...and no. I guess I want to print out the 'code' for any address
so that I can get the data corrected but I guess I would also like to
remove those records at the /dataroot/address level so they don't
appear in the file.
[more code using XML::LibXML snipped]
XML::Simple isn't so bad :-)
XML::Twig neither ;->
#!/usr/bin/perl
use warnings;
use strict;
use XML::Twig;
my $xml;
$xml .= $_ while <DATA>;
my $twig = XML::Twig->new(
twig_handlers => {
'address' => sub {
my $elt = $_;
for ($elt->descendants('line')) {
if ($_->text =~ /\bNOT\b/) {
print "Invalid address number: "
. $elt->{att}->{number} . ", code: "
. $elt->first_child('code')->text . "\n";
$elt->cut;
}
}
}
},
pretty_print => 'indented',
)->parse($xml);
$twig->print;
__DATA__
<dataroot>
<address number="1016">
<code>B679OOO00</code>
<record_type>client</record_type>
<address_type>shipping</address_type>
<Postcode></Postcode>
<Country>GBR</Country>
<lines>
<line>DO NOT USE THIS CODE</line>
</lines>
</address>
<address number="1014">
<code>P982LUS00</code>
<record_type>client</record_type>
<address_type>shipping</address_type>
<Postcode>HR2 0AU</Postcode>
<Country>GBR</Country>
<lines>
<line>UPPER HOUSE FARM</line>
<line>BACTON</line>
<line>ESSEX</line>
<line>EX2 0AU</line>
</lines>
</address>
<address number="1333">
<code>A234ULE00</code>
<record_type>client</record_type>
<address_type>shipping</address_type>
<Postcode></Postcode>
<Country>AND</Country>
<lines>
<line>QUEENS HOUSE</line>
<line>1 BUCKINGHAM PALACE</line>
<line>LONDON WC2H</line>
<line>****NOT AT THIS ADDRESS ANY MORE.</line>
<line>***************</line>
</lines>
</address>
<address number="1018">
<code>A&MPUB00</code>
<record_type>client</record_type>
<address_type>shipping</address_type>
<Postcode>PO19 8SQ</Postcode>
<Country>GBR</Country>
<lines>
<line>THE ATRIUM</line>
<line>SOUTHERN GATE</line>
<line>CHICHESTER</line>
<line>SUSSEX</line>
<line>PO19 8SQ</line>
</lines>
</address>
</dataroot>
HTH,
Andreas Puerzer
--
perl -mAcme::JAPH
.
- References:
- XML::LibXML navigation
- From: Beginner
- Re: XML::LibXML navigation
- From: Rob Dixon
- Re: XML::LibXML navigation
- From: Beginner
- Re: XML::LibXML navigation
- From: "Mumia W."
- XML::LibXML navigation
- Prev by Date: Capitureing error messages
- Next by Date: Re: Calling a perl program from a perl script
- Previous by thread: Re: XML::LibXML navigation
- Next by thread: Re: XML::LibXML navigation
- Index(es):
Relevant Pages
|