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