我有以下 XML结构: RECORDTYPE ID="check_ping"HOSTSHOST H="192.168.0.1" W="50,1%" C="100,1%" /HOST H="192.168.0.2" W="50,1%" C="70,1%" //HOSTS/TYPETYPE ID="check_ssh"HOSTSHOST H="192.168.0.3" P="21"/HOST H="192.168.0.4" P="21"/HOST H=
<RECORD> <TYPE ID="check_ping"> <HOSTS> <HOST H="192.168.0.1" W="50,1%" C="100,1%" /> <HOST H="192.168.0.2" W="50,1%" C="70,1%" /> </HOSTS> </TYPE> <TYPE ID="check_ssh"> <HOSTS> <HOST H="192.168.0.3" P="21"/> <HOST H="192.168.0.4" P="21"/> <HOST H="192.168.0.4" P="21"/> </HOSTS> </TYPE> .... </RECORD>
我还有一个应该读取XML的Perl脚本,我正在使用XML :: XPath模块.
use XML::XPath; #..... my $xp = XML::XPath->new(filename => 'test.xml'); #RETRIEVE ALL HOSTS WITH CHECK_SSH my $querysshh = '/RECORD/TYPE[@ID="check_ssh"]/HOSTS/HOST'; $nodeset = $xp->find($querysshh); foreach $mynode ($nodeset->get_nodelist) { #HOW DO I RETRIEVE ATTRIBUTES NOW? } #.....
如何检索这些节点的属性?例如,如果我想读H和P我需要做什么?我查看了模块文档,但我找不到任何有用的方法.
尝试$myNode-> getAttribute(“H”)是否有效.