Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jsoup best method to get an Element from a string

Tags:

java

jsoup

How can I obtain a Jsoup Element from a String? For example if I have a String

String myDiv = "<div>Hello jsoup world</div>";

that I want to convert in an Element. Currently I convert the String in a Document with the Jsoup.parse(..) method and then I get the body of that document as Element. Is there a direct method?

like image 747
user2572526 Avatar asked Oct 31 '13 14:10

user2572526


1 Answers

You can use the XML-Parser instead the HTML one:

final String html = "<div>Hello jsoup world</div>";

Document doc = Jsoup.parse(html, "", Parser.xmlParser());
Element tag = doc;

Or shorter:

Element tag = Jsoup.parse(html, "", Parser.xmlParser());
like image 145
ollo Avatar answered Sep 28 '22 07:09

ollo