1

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&nbsp;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 &nbsp;

4

1 回答 1

5

jSoup works correctly, you have a problem with output encoding.

In Windows, character encoding used by console (CP437 in your case) is not the same as the system encoding (Windows-1252 in 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:

System.console().writer().println(linkText); 
于 2011-01-03T18:04:27.230 回答