Is this a valid UTF8 character in this xml file?

半世苍凉 提交于 2019-12-11 01:48:23

问题


I have received some XML from an upstream data source.

I'm not sure if these weird characters are valid UTF8 -or- the upstream source has messed things up. i.e. Bad data in => bad data out.

I'm guessing the following is what was passed down:

Value in XML file  | Unicode Value | UTF-8 Value  | English Description
-------------------------------------------------------------------------------------------
’ | U+2019        | \xe2\x80\x99 | RIGHT SINGLE QUOTATION MARK
• | U+2022        | \xe2\x80\xa3 | BULLET
&              | -not unicode- | --           | Ampsersand, HTML Encoded.

i feel like the \ at the start of the UFT-8 value is sorta... encoded but .. done wrong?

Can someone please explain what I'm looking at, so I know how to correctly decode it. What's also frustrating is that i feel like this could be a mix of encodings which will make things awful :(

Reference: http://utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128&utf8=string-literal


回答1:


It's not a matter of UTF-8 in the XML you receive because character escapes of the &#xXX; encode characters and so there's no question of what the encoding is. [Actually, it could be this, in that it could be that whatever is producing the XML was written by someone who doesn't understand how XML escapes are meant to work. After all, once something is buggy, there's no point assuming it does anything correctly until proven otherwise.]

It does look like something along the way has treated some perfectly good UTF-8 as if it was a different encoding, then decided to escape the results. Some of the characters you are getting as a result of this ('U+0080' and 'U+0099') are characters that are allowed in XML but strongly discouraged. Some ('â' and '¢') are perfectly sensible characters (though produced in non-sensible ways) that makes the decision to escape it nearly as strange as whatever mistake led to their being there.

Whatever the source of the mojibake, you're getting mojibake, so if you can complain or report a bug upstream, do so and have it fixed at source rather than trying to fix something that is broken.

Otherwise you're going to have to try to unescape the characters, encode them as if they were whatever format they thought they were (I'd guess ISO Latin 1, but there are other possibilities) and then decode them as if they were UTF-8. There's no promise that that won't do just as much damage to a correct bit of the document as it undoes to that buggy bit though.



来源:https://stackoverflow.com/questions/46048351/is-this-a-valid-utf8-character-in-this-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!