简体   繁体   English

如何在jsoup中获取元素的第一级子元素

[英]How to get first-level children of an element in jsoup

In jsoup Element.children() returns all children (descendants) of Element. 在jsoup中, Element.children()返回Element的所有子项(后代)。 But, I want the Element's first-level children (direct children). 但是,我想要Element的一级孩子(直接孩子)。

Which method can I use? 我可以使用哪种方法?

Element.children() returns direct children only. Element.children()仅返回直接子项。 Since you get them bound to a tree, they have children too. 既然你把它们绑在树上,它们也有孩子。

If you need the direct children elements without the underlying tree structure then you need to create them as follows 如果您需要没有底层树结构的直接子元素,则需要按如下方式创建它们

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

Output 产量

2
ul
p

divChildren content: 
<ul>
 <li>11</li>
 <li>22</li>
</ul>
<p>ppp<span>sp</span></p>

detachedDivChildren content: 
<ul></ul>
<p></p>

This should give you the desired list of direct descendants of the parent node: 这应该为您提供父节点的直接后代所需的列表:

Elements firstLevelChildElements = doc.select("parent-tag > *");

OR You can also try to retrieve the parent element, get the first child node via child(int index) and then try to retrieve siblings of this child via siblingElements() . 或者您也可以尝试检索父元素,通过child(int index)获取第一个子节点,然后尝试通过siblingElements()检索此子节点的兄弟siblingElements()

This will give you the list of first level children excluding the used child, however you'd have to add the child externally. 这将为您提供排除使用过的孩子的第一级孩子的列表,但是您必须在外部添加孩子。

Elements firstLevelChildElements = doc.child(0).siblingElements();

您始终可以将ELEMENT.child(index)与索引一起使用,您可以选择所需的子项。

Here you can get the value of first-level children 在这里,您可以获得一级孩子的价值

 Element addDetails = doc.select("div.container > div.main-content > div.clearfix > div.col_7.post-info > ul.no-bullet").first();
    Elements divChildren = addDetails.children();
    for (Element elem : divChildren) {
       System.out.println(elem.text());
                }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM