Re: XML::LibXML navigation
- From: dermot@xxxxxxxxxxxxxxxx (Beginner)
- Date: Fri, 12 Jan 2007 17:29:08 -0000
On 12 Jan 2007 at 17:06, Rob Dixon wrote:
Hi Rob,
#!/usr/bin/perl
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 ==========
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 @codes = $doc->findnodes('//code');
my @lines = $doc->findnodes('//lines');
for (my $i = 0; $i < $#codes; ++$i) {
#print $codes[$i]->string_value, "\t";
my @add = $lines[$i]->childNodes;
for ( my $a = 1; $a <$#add; ++$a) {
if ($add[$a]->string_value =~ /\s+NOT\s+/) {
print $codes[$i]->string_value,": ",$add[$a]-
string_value,"\n";}
}
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?
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.
i spent a lot of time on this today as this look like a excellent
parser and DOM navigator but I struggled moving around.
In your example @results looks like it would contain references to
all the /lines/line data with DO NOT USE in the string_value. What I
have struggling with is that this is also a reference to the record
as a whole and my navigation techniques are not working out. For
example whenever I used findnodes I was getting every code in the
file. I think now that was because I was using /dataroot/address as
the starting point.
Aside from CPAN, I would appreciate any other sources of info about
using the using libXML with perl and xpath expressions. It is
whoppingly fast.
Thanx again,
Dp.
Rob
.
- Follow-Ups:
- Re: XML::LibXML navigation
- From: "Mumia W."
- Re: XML::LibXML navigation
- From: Rob Dixon
- Re: XML::LibXML navigation
- References:
- XML::LibXML navigation
- From: Beginner
- Re: XML::LibXML navigation
- From: Rob Dixon
- XML::LibXML navigation
- Prev by Date: Re: XML::LibXML navigation
- Next by Date: RE: Java 1.6 support to Perl
- Previous by thread: Re: XML::LibXML navigation
- Next by thread: Re: XML::LibXML navigation
- Index(es):
Relevant Pages
|