简体   繁体   English

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

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

Suppose I have a XML file 假设我有一个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>

I want to get the child elements of the table element. 我想获取table元素的子元素。 ie the output should be person , student and employee . 即输出应该是personstudentemployee How do I do this with the XML::LibXML module in Perl? 我该如何在Perl中使用XML::LibXML模块?

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用XML :: LibXML:如何创建名称空间和子元素并使它们一起工作? - Using XML::LibXML: How do I create namespaces and child elements and make them work together? 如何使用Perl模块XML :: LibXML替换XML中类似的父节点列表中的特定子节点值 - How to replace specific child node values among similar parent node lists in XML using Perl module XML::LibXML 如何使用XML :: LibXML查找不区分大小写的节点 - How to find nodes case-insensitive using XML::LibXML XML:使用LibXML查找XML中的特定节点 - XML:Find particular node in XML using LibXML 如何使用父元素和子元素将 XML 导入 MySQL? - How to import XML to MySQL with parent and child elements? 如何使用libxml2获取这些XML元素? - how to get these XML elements with libxml2? 使用XML :: LibXML查找和替换文本 - Find and replace text using XML::LibXML 如何使用 python 将父元素的属性传递给 XML 中的子元素? - How can I transfer the attributes of parent elements to child elements in XML using python? 如何使用XElement在xml中找到根元素之一的值? 当根元素没有子元素时? - How to find value of one of root elements in xml using XElement? when the root elements have no child elements? 如何在XML中找到与公共父元素相同的元素? - How to find equal elements with common parent in XML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM