gpt4 book ai didi

java - 统计特定节点下的xml节点

转载 作者:数据小太阳 更新时间:2023-10-29 02:52:05 24 4
gpt4 key购买 nike

<element>
<slot name="slot1">
<port status="active"/>
<port status="inactive"/>
</slot>
<slot name="slot2">
<port status="active"/>
</slot>
</element>

NodeList listOfElement = doc.getElementsByTagName("port"); 这给出了 xml 文件中的端口总数。

我需要找出 slot1 下有多少个端口。

最佳答案

一种方法是使用 xpath 过滤掉结果列表:

XPath xpath = XPathFactory.newInstance().newXPath();            
NodeList list=(NodeList) xpath.evaluate("/element/slot[@name='slot1']/port",doc, XPathConstants.NODESET);

这只会计算名称为 'slot1' 的插槽下的端口。

关于java - 统计特定节点下的xml节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7228182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com