Java - 转换Html特殊字符的问题

时间:2013-06-07 12:53:21

标签: java xpath htmlcleaner

我正在尝试使用带有JAVA的Xpath来解析HTML页面。这是我的代码:

        /** Cleaning the html file */
        /** the 'doc' variable is a String containing the whole html file */
        TagNode tagNode = new HtmlCleaner().clean(doc);
        Document doc2 = new DomSerializer( new CleanerProperties() ).createDOM(tagNode);




        XPath xpath = XPathFactory.newInstance().newXPath();

        /** xpath request */
        Object dates_experience = xpath.evaluate("/html/body/div[3]/div/div/div[2]/div/div/div[2]/div[4]/div/div[3]/h4/span[2]", doc2, XPathConstants.NODESET);

        NodeList nodes = (NodeList) dates_experience;
        String s;
        for (int i = 0; i < nodes.getLength(); i++) {
            s = org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(nodes.item(i).getTextContent());
            System.out.println(s); 
        }

我认为我有使用stringEscapeUtils或使用HtmlCleaner的探针,因为在输出中,我看到了:

�

而不是那些字符:

é, è, ', à, û, ...etc

例如,我有这个输入:

décembre 2010 - décembre 2010)
février 2010 - juin 2010)
juillet 2009 - septembre 2009)
juin 2009 - juin 2009)
juillet 2008 - août 2008)

我的程序产生了这个输出:

d�cembre 2010 - d�cembre 2010)
f�vrier 2010 - juin 2010)
juillet 2009 - septembre 2009)
juin 2009 - juin 2009)
juillet 2008 - ao�t 2008)
你可以帮我解决这个问题吗?

感谢。

1 个答案:

答案 0 :(得分:1)

我怀疑你应该*逃避,不要逃避它们:StringEscapeUtils.unescapeHtml4(String)