Reputation: 195
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
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
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