Element.children()仅返回直接子代。由于您将它们绑定到树上,因此它们也有孩子。
如果需要直接子元素而没有底层树结构,则需要按以下方式创建它们
public static void main(String... args) { document document = Jsoup .parse("<div><ul><li>11</li><li>22</li></ul><p>ppp<span>sp</span</p></div>"); Element div = document.select("div").first(); Elements divChildren = div.children(); Elements detachedDivChildren = new Elements(); for (Element elem : divChildren) { Element detachedChild = new Element(Tag.valueOf(elem.tagName()), elem.baseUri(), elem.attributes().clone()); detachedDivChildren.add(detachedChild); } System.out.println(divChildren.size()); for (Element elem : divChildren) { System.out.println(elem.tagName()); } System.out.println("ndivChildren content: n" + divChildren); System.out.println("ndetachedDivChildren content: n" + detachedDivChildren);}
输出量
2ulpdivChildren content: <ul> <li>11</li> <li>22</li></ul><p>ppp<span>sp</span></p>detachedDivChildren content: <ul></ul><p></p>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)