Sea Citadel
Sea Citadel

Reputation: 195

How to write an ampersand character in 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?

Upvotes: 4

Views: 1668

Answers (2)

Adithya Surampudi
Adithya Surampudi

Reputation: 4454

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.");

Upvotes: 4

orien
orien

Reputation: 2200

Try using the unicode value for no-breaking space.

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

Upvotes: 2

Related Questions