Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an ampersand character in Jsoup

Tags:

java

html

jsoup

I'm using Jsoup to parse and modify some HTML. In certain places, I want to add a non-breaking space entity ( ) to the HTML. I assumed I could do it as in this simplified example:

Element paragraph = someDocument.select("p").first();
paragraph.text("First sentence.  Second sentence.");

But Jsoup turns my   into   effectively encoding the ampersand itself. I guess my real question is: how can I manually write an ampersand character to the text of an Element?

like image 789
Knave Avatar asked Oct 15 '25 03:10

Knave


1 Answers

You are doing Element.text. If its html, use .html(String s) instead so, replace your code with

Element paragraph = someDocument.select("p").first();
paragraph.html("First sentence.  Second sentence.");
like image 173
Adithya Surampudi Avatar answered Oct 19 '25 19:10

Adithya Surampudi