문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top