簡體   English   中英

如何使用XML :: LibXML查找指定父對象的子元素?

[英]How to find child elements of a specified parent using XML::LibXML?

假設我有一個XML文件

<table>
    <person>
        <ID>1</ID>
        <Name>Adam</Name>
    </person>
    <student>
        <Subject>Math</Subject>
        <Marks>90</Marks>
    </student>
    <employee>
        <ID>7</ID>
        <Name>Bill</Name>
    </employee>
</table>

我想獲取table元素的子元素。 即輸出應該是personstudentemployee 我該如何在Perl中使用XML::LibXML模塊?

for my $node ($doc->findnodes('/table/*')) {
    say $node->nodeName();
}

要么

use XML::LibXML qw( XML_ELEMENT_NODE );

my $root = $doc->documentElement();
for my $node ( grep { $_->nodeType() == XML_ELEMENT_NODE } $root->childNodes() ) {
    say $node->nodeName();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM