XML:仅获取节点的一些子节点

Posted

技术标签:

【中文标题】XML:仅获取节点的一些子节点【英文标题】:XML: Get only some child nodes of a node 【发布时间】:2019-04-30 03:52:45 【问题描述】:

我想将下面 xml 中的两个会话作为两个单独的节点进行遍历:

abc.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<resource>    
<session>
     <parameter name="QWS" value="N1"/> 
     <parameter name="AQC" value="Y2"/>
     <parameter name="VDF" value="301"/>
     <parameter name="VTT" value="X2"/>
     <parameter name="HRR" value="Y2"/>
 </session>
 <session>
    <parameter name="ABC" value="N"/> 
     <parameter name="XYZ" value="Y"/>
     <parameter name="GHI" value="30"/>
     <parameter name="DEF" value="X"/>
     <parameter name="QQW" value="Y"/>  
    </session> 
</resource>

但是,当我运行此代码时,它会遍历两个会话:

#!/usr/bin/perl
use warnings;
use strict;
use XML::LibXML;

my $filename = 'E:/perl_files/abc.xml';
my $dom = XML::LibXML->load_xml(location => $filename);

foreach my $session ($dom->findnodes('//session'))

    print "In SESSION \n";
    foreach my $parameter ($session->findnodes('//parameter')) 
        my $name= $parameter->getAttribute('name');
        my $value= $parameter->getAttribute('value');
        print "Tag in session is $name with value $value\n";
    
    print "END OF SESSION\n";   

留下这个输出:

In SESSION
Tag in session is QWS with value N1
Tag in session is AQC with value Y2
Tag in session is VDF with value 301
Tag in session is VTT with value X2
Tag in session is HRR with value Y2
Tag in session is ABC with value N
Tag in session is XYZ with value Y
Tag in session is GHI with value 30
Tag in session is DEF with value X
Tag in session is QQW with value Y
END OF SESSION
In SESSION
Tag in session is QWS with value N1
Tag in session is AQC with value Y2
Tag in session is VDF with value 301
Tag in session is VTT with value X2
Tag in session is HRR with value Y2
Tag in session is ABC with value N
Tag in session is XYZ with value Y
Tag in session is GHI with value 30
Tag in session is DEF with value X
Tag in session is QQW with value Y
END OF SESSION

我希望输出是

In SESSION
Tag in session is QWS with value N1
Tag in session is AQC with value Y2
Tag in session is VDF with value 301
Tag in session is VTT with value X2
Tag in session is HRR with value Y2
END OF SESSION
In SESSION
Tag in session is ABC with value N
Tag in session is XYZ with value Y
Tag in session is GHI with value 30
Tag in session is DEF with value X
Tag in session is QQW with value Y
END OF SESSION

我正在使用 XML::LibXML。 我可以进行哪些更改以每次仅获取一个会话的子节点? 我看到了使用 childNodes() 的答案,但这会返回一个列表,我不想使用 grep 来查找特定节点。

【问题讨论】:

【参考方案1】:

//parameter到处搜索parameter,只需要在session下搜索,即当前节点下:

    foreach my $parameter ($session->findnodes('.//parameter')) 

【讨论】:

工作就像一个魅力!非常感谢!【参考方案2】:
//parameter

简称

/descendant::parameter

表示根节点 (/) 的后代名为 parameter,但您想获取上下文节点的后代。为此,你想要

descendant::parameter

也可以写成

.//parameter

问题是,parameter 是会话的孩子,所以你真正想要的只是

child::parameter

也可以写成

parameter

最终代码:

#!/usr/bin/perl
use warnings;
use strict;
use XML::LibXML;

my $filename = 'E:/perl_files/abc.xml';
my $dom = XML::LibXML->load_xml(location => $filename);

foreach my $session ($dom->findnodes('/resource/session'))         # <---

    print "In SESSION \n";
    foreach my $parameter ($session->findnodes('parameter'))      # <---
        my $name= $parameter->getAttribute('name');
        my $value= $parameter->getAttribute('value');
        print "Tag in session is $name with value $value\n";
    
    print "END OF SESSION\n";   

【讨论】:

以上是关于XML:仅获取节点的一些子节点的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 XPATH for XML 获取子节点数

是元素节点的属性节点子节点

如何反序列化xml文件的子节点

ztree获取当前选中节点子节点id集合的方法(转载)

jQuery获取父节点子节点兄弟节点

如何从父 yaml 节点获取完整的 yaml 节点子对象?