我有一个 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 语句的问题吗?谢谢。
最佳答案
不要在循环中使用 findnodes
,而是使用 getElementsByTagName() :
my @c = $nod->getElementsByTagName('rawData');
以下是对 @c
数组进行处理的其他一些便捷方法:
$c[0]->toString; # <rawData>0.1123975676</rawData>
$c[0]->nodeName; # rawData
$c[0]->textContent; # 0.1123975676
关于perl - 为什么我无法使用 XML::LibXML 中的 XPath 访问 XML 文件内的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2079668/