从HTML Java读取时保留特殊字符? [英] Retaining special character while reading from html java?

查看:121
本文介绍了从HTML Java读取时保留特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取html源文件,其中包含诸如äöü€€的德语字符

i am trying to read html source file which contains German characters like ä ö ü ß €

使用JSOUP进行阅读

Reading using JSOUP

citAttr.nextElementSibling().text() 

使用

unicodeEscaper.translate(citAttr.nextElementSibling().text())

org.apache.commons.lang3.text.translate.UnicodeEscaper

问题是在读取字符后变成

Issue is after reading the charecters turns into �

但是在读取Unicode类型为UTF-8的CSV以及上面的unicodeEscaper保存和保存的地方检索字符可以正常工作.

But where as reading CSV with Encoded type UTF-8 with above unicodeEscaper saving & retriving the charecters works fine.

unicodeEscaper.translate(record.get(headerPosition.get(0)))

从html读取有什么问题?尝试使用StringUtilEscaper方法仍然将字符转换为

Whats the issue with reading from html ?? did try with StringUtilEscaper methods still the charecters turns into �

private String getText(Part p) throws MessagingException, IOException {
    if (p.isMimeType("text/*")) {
        String s = (String) p.getContent();
        textIsHtml = p.isMimeType("text/html");
        return s;
    }

这是我阅读具有html内容的电子邮件的方式!

This is how i am reading email which have html content!

推荐答案

我今天刚刚回答了一个类似的问题...我想我可以输入关于扩展字符集(外语字符)的知识,因为那是一个我编写的软件的主要方面.

I just answered a similar question today... I guess I can just type what I know about extended character sets (foreign-language characters), since that's one of the major facets of the software I write.

  • Java内部的 String's 均使用 16-bit chars (原始类型 char 是一个16位原始值.名称 UTF-8 有点误导,因为它用来表示16位的"Unicode空间"(使用两个8位数字).这意味着Java(和Java String's ) 代表整个Unicode外语字母范围没有问题.
  • JSoup,以及几乎所有用Java编写的HTML工具,当要求下载网站页面时,都将返回16位字符-作为Java String's -很好,没有任何问题!如果查看这些范围时遇到问题,则可能不是下载过程,也不是JSoup或HttpUrlConnection设置.当您将网页保存到Java中的字符串中时,您并没有丢失那些字符,实际上您会得到 UTF-8 免费".
  • 方法:每当程序员尝试将 UTF-8 String 保存到 '.txt' File '.html' File ,然后继续在网络浏览器中查看该内容(该文件),您可能会看到的只是那个烦人的问号:.这是因为您需要确保让您的Web浏览器知道使用Java保存的 '.html' File -不能用(更旧,更多较短) 8-bit ASCII 范围.
  • Java's internal String's all use 16-bit chars (The primitive type char is a 16-bit primitive value. The name UTF-8 is a little misleading since it is used to represent the 16-bit "Unicode Space" (using two 8-bit numbers). This means that Java (and Java String's) have no problems representing the entire Unicode foreign-language alphabet ranges.
  • JSoup, and just about any HTML tool written in Java, when asking for website pages to download, will return 16-bit characters - as Java String's - just fine, without any problems! If there are problems viewing these ranges, it is likely not the download process, nor a JSoup or HttpUrlConnection setting. When you save a web-page to a String in Java, you haven't lost those characters, you essentially get UTF-8 "for free."
  • HOWEVER: Whenever a programmer attempts to save a UTF-8 String to a '.txt' File or a '.html' File, if you then go on to view that content (that file) in a web-browser, all you might see is that annoying question mark: �. This is because you need to make sure to let your web-browser know that the '.html' File that you have saved using Java - is not intended to be interpreted using the (much older, much shorter) 8-bit ASCII Range.

如果您在任何网络浏览器中查看 '.html' File ,或将该文件上传到Google Cloud Platform(或某些托管网站),则必须执行以下两项操作之一:

If you view an '.html' File in any web-browser, or upload that file to Google Cloud Platform (or some hosting site), you must do one of two things:

  • 在注释中包括 <META> Tag :HTML页面的<HEAD> ... </HEAD>部分中的 <meta charset="UTF-8"> .
  • 或者在您必须将文件标识为'text/html, charset=UTF-8'的任何托管平台中提供设置.在Google Cloud Platform存储桶中,有一个弹出菜单,用于将此设置分配给任何文件.
  • Include the <META> Tag mentioned in the comments: <meta charset="UTF-8"> in the HTML Page's <HEAD> ... </HEAD> section.
  • Or provide the setting in whatever hosting platform you have to identify the file as 'text/html, charset=UTF-8'. In Google Cloud Platform Storage Buckets there is a popup menu to assign this setting to any file.

这篇关于从HTML Java读取时保留特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆