In Jsoup, how to create an element without document?

10,458

Solution 1

Try to use Elements constructor:

Element el = new Element(Tag.valueOf("ol"), "");

Solution 2

Since version 1.10.2, You can use Element's constructor like this:

Element el = new Element("ol");

Constructor and Description:

public Element(String tag)
Create a new, standalone element.
Parameters:
tag - tag name

Share:
10,458
CL So
Author by

CL So

Updated on June 28, 2022

Comments

  • CL So
    CL So almost 2 years

    Now I use this to create an element

    <!-- language: lang-java -->
    
            Document doc = Jsoup.parseBodyFragment("<ol></ol>");
            Elements ols=doc.getElementsByTag("ol");
            Element ol=ols.get(0);
    

    But this is too complex, because I create many dom in the program, if every use three line to create an element, it is not convenient.

    Can I create an element without using the document and elements?

    like this:

    <!-- language: lang-js -->
    var ol=$('<ol></ol>');