简体   繁体   中英

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

Suppose I have a XML file

<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>

I want to get the child elements of the table element. ie the output should be person , student and employee . How do I do this with the XML::LibXML module in Perl?

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

or

use XML::LibXML qw( XML_ELEMENT_NODE );

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM