Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Jsoup, how to create an element without document?

Tags:

java

jsoup

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>');
like image 965
CL So Avatar asked Sep 02 '14 08:09

CL So


2 Answers

Try to use Elements constructor:

Element el = new Element(Tag.valueOf("ol"), "");
like image 199
Aleksei Shestakov Avatar answered Sep 23 '22 03:09

Aleksei Shestakov


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

like image 26
Yim Avatar answered Sep 21 '22 03:09

Yim