Beginner wrote:
Hi,
I have to do some sanity checks on a large xml file of addresses
(snip below). I have been using XML::LibXML and seem to have started
ok but I am struggling to navigate around a record.
In the sample date below your'll see some addresses with "DO NOT..."
in. I can locate them easily enough but I am struggling to navigate
back up the DOM to access the code so I can record the code with
faulty addresses.
Here my effort. Can anyone help me either to move backup up to the
right element node or catch the code node before I begin to loop
through the address line(s).
TIA,
Dp.
======= My Effort ==========
#!/usr/bin/perl
use strict;
use warnings;
use XML::LibXML;
my $file = 'ADDRESS.XML';
open(FH,$file) or die "Can't open file $file: $!\n";
my $parser = XML::LibXML->new;
my $doc = $parser->parse_fh(\*FH);
my @results = $doc->findnodes('//address');
foreach my $i (@results) {
my @addlines = $i->findnodes('//line');
foreach my $l (@addlines) {
if ($l->string_value =~ /\s+NOT\s+/) {
my $p = $i->nodePath;
$p .= '/code';
print $p->nodeValue,"\t";
print $l->string_value, "\t";
print $l->string_value, "\n";
}
}
}
[snip XML]
If I understand you correctly then all you need is
my @results = $doc->findnodes('/dataroot/address[contains(lines/line, "DO NOT
USE")]');
foreach my $address (@results) {
my $code = $address->findvalue('code');
print $code, "\n";
}
which prints the code of all those addresses that have a line containing 'DO NOT
USE'. Is that what was required?
(Note that I've assumed a root node <dataroot>. You will need to change the node
name to the actual value.)
HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/