我正在使用 XML :: Lib XML(Ver:1.70). 我的xml输入文件如下所示: ?xml version="1.0" encoding="UTF-8"?Equipment xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Equipments ECI
我的xml输入文件如下所示:
<?xml version="1.0" encoding="UTF-8"?> <Equipment xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Equipments> <ECID logicalName="SysNameAlpha" id="0"/> <ECID logicalName="SysNameBeta" id="1"/> </Equipments> </Equipment>
和我的Perl脚本:
my $file = 'data.xml'; my $parser = XML::LibXML->new(); my $tree = $parser->parse_file($file); my $root = $tree->getDocumentElement; foreach my $camelid ($root->findnodes('Equipments')) { my $name = $camelid->findvalue('ECID/@logicalName'); my $id = $camelid->findvalue('ECID/@id'); print $name; print " = "; print $id; print ";\n"; }
我得到的输出是:
SysNameAlphaSysNameBeta = 01;
但我想要这样的输出:
SysNameAlpha = 0; SysNameBeta = 1;
我怎样才能做到这一点?
只有一个Equipments节点,因此您只能获得一个$camelid进行扫描.要进行补救,您可能会稍微改变一下,例如,迭代设备/ ECID:foreach my $camelid ( $root->findnodes('Equipments/ECID') ) { my $name = $camelid->findvalue('@logicalName'); my $id = $camelid->findvalue('@id'); ... }