views:

176

answers:

2

I have an XML file, part of which looks like this:

 <wave waveID="1">
    <well wellID="1" wellName="A1">
      <oneDataSet>
        <rawData>0.1123975676</rawData>
      </oneDataSet>
    </well>
    ... more wellID's and rawData continues here...

I am trying to parse the file with Perl's libXML and output the wellName and the rawData using the following:

    use XML::LibXML;
    my $parser = XML::LibXML->new();
    my $doc = $parser->parse_file('/Users/johncumbers/Temp/1_12-18-09-111823.orig.xml');
    my $xc = XML::LibXML::XPathContext->new( $doc->documentElement()  );
    $xc->registerNs('ns', 'http://moleculardevices.com/microplateML');

            my @n = $xc->findnodes('//ns:wave[@waveID="1"]');   #xc is xpathContent
        # should find a tree from the node representing everything beneath the waveID 1
        foreach $nod (@n) {
            my @c = $nod->findnodes('//rawData');  #element inside the tree.
            print @c;
        }

It is not printing out anything right now and I think I have a problem with my Xpath statements. Please can you help me fix it, or can you show me how to trouble shoot the xpath statements? Thanks.

+1  A: 

Instead of using findnodes in the loop, use getElementsByTagName():

my @c = $nod->getElementsByTagName('rawData');

Here are some other handy methods to use processing to @c array:

$c[0]->toString;    # <rawData>0.1123975676</rawData>
$c[0]->nodeName;    # rawData
$c[0]->textContent; # 0.1123975676
martin clayton
+1  A: 

If the 'wave' element is in a namespace then the 'rawData' element is as well so you probably need to use

foreach $nod (@n) {
    my @c = $xc->findnodes('descendant::ns:rawData', $nod);  #element inside the tree.
    print @c;
}
Martin Honnen