I have a little sample program which extracts some information from an HTML document.
import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class TestSoup {
public static void main(String[] args) {
String html = "<p>An <a href='http://example.com/'><b>exa mple</b></a> link.</p>";
Document doc = Jsoup.parse(html);
Element link = doc.select("a").first();
String linkText = link.text(); // "example""
System.out.println(linkText);
}
}
If you’ve worked with jSOup you’ll know that the output of this should be exa mple but the output is exaámple. Why is jSoup not unescapting my HTML entities properly or am i simply doing this wrong?
All my HTML entities get unescaped incorrectly and not only
jSoup works correctly, you have a problem with output encoding.
In Windows, character encoding used by console (
CP437in your case) is not the same as the system encoding (Windows-1252in your case).System.out.println()outputs your string in the system default encoding, therefore it’s incorrectly displayed in console.In Java 1.6 you can try
System.console()instead: