Re: XML::LibXML navigation



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
.



Relevant Pages

  • Re: XML::LibXML navigation
    ... 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. ... foreach my $address { ... Using findnodes and specifying an XPath statement only seems to pick up the first line in the lines element. ...
    (perl.beginners)
  • XML::LibXML navigation
    ... I have to do some sanity checks on a large xml file of addresses ... I can locate them easily enough but I am struggling to navigate ... right element node or catch the code node before I begin to loop ... foreach my $l { ...
    (perl.beginners)
  • Re: csproj/vbproj definition available?
    ... and A Project represents an MSBuild ... save to an XML file, preserving most whitespace and all XML comments." ... foreach ... foreach (BuildItemGroup itemGroup in project.ItemGroups) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Best thing to simulate an Ini file
    ... > Use a simple class to read and write to a configuration file on either the ... Here's a sample of the xml file and a class to ... > foreach (XmlNode node in configDocument.ChildNodes) ...
    (microsoft.public.dotnet.framework.compactframework)
  • Webbrowser.navigate() not firing
    ... Basically all this script does is create an xml output file. ... DocumentCompleted ALWAYS fires (as if the navigate ... new xml file is created. ...
    (microsoft.public.dotnet.languages.csharp)