2

我有一个 XML 文件,其中一部分如下所示:

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

我正在尝试使用 Perl 的 libXML 解析文件并使用以下命令输出 wellName 和 rawData:

    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;
        }

它现在没有打印出任何东西,我认为我的 Xpath 语句有问题。请你能帮我修复它,或者你能告诉我如何解决xpath语句吗?谢谢。

4

2 回答 2

2

不要findnodes在循环中使用,而是使用getElementsByTagName()

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

@c以下是使用处理数组的其他一些方便的方法:

$c[0]->toString;    # <rawData>0.1123975676</rawData>
$c[0]->nodeName;    # rawData
$c[0]->textContent; # 0.1123975676
于 2010-01-17T02:50:00.793 回答
2

如果“wave”元素在命名空间中,那么“rawData”元素也是如此,因此您可能需要使用

foreach $nod (@n) {
    my @c = $xc->findnodes('descendant::ns:rawData', $nod);  #element inside the tree.
    print @c;
}
于 2010-01-17T11:54:25.973 回答