开发者

jSoup isn't unescaping my HTML entities properly

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 &开发者_如何学Cnbsp;


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); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜