gpt4 book ai didi

java - XML 解析 : counting the number of sub elements

转载 作者:搜寻专家 更新时间:2023-11-01 09:06:19 24 4
gpt4 key购买 nike

我是 XML 的新手。我有如下所示的 XML 输出,

<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:srch" xsi:schemaLocation="urn:yahoo:srch http://search.yahooapis.com/WebSearchService/V1/WebSearchRelatedResponse.xsd">
<Result>london olympics 2012</Result>
<Result>olympics schedule</Result>
<Result>olympics closing ceremony</Result>
<Result>winter olympics</Result>
<Result>nbc olympics</Result>
<Result>2016 olympics</Result>
<Result>olympics opening ceremony</Result>
<Result>special olympics</Result>
<Result>2008 olympics</Result>
<Result>2020 olympics</Result>
</ResultSet>

我需要统计父标签中标签的数量。

NodeList nl = doc.getElementsByTagName("ResultSet");

上面给出了一个单一的元素对象。如何知道这组结果有多少?

谢谢!拉胡尔。

最佳答案

获取集合的大小:

nl.getLength()

public int getLength()

The number of nodes in the list. The range of valid child node indices is 0 to length-1 inclusive.

Node List

关于java - XML 解析 : counting the number of sub elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11670317/

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