Retaining Special Character While Reading From Html Java?
i am trying to read html source file which contains German characters like ä ö ü ß € Reading using JSOUP citAttr.nextElementSibling().text() Encoding the string with unicod
Solution 1:
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's internal
String's
all use16-bit chars
(The primitive typechar
is a 16-bit primitive value. The nameUTF-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 JavaString'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 orHttpUrlConnection
setting. When you save a web-page to a String in Java, you haven't lost those characters, you essentially getUTF-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.
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:
- 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.
Post a Comment for "Retaining Special Character While Reading From Html Java?"