这个字符是什么意思以及如何解码这些类型的字符?

What is this character mean and how to decode these type of characters?

提问人:Srinivas 提问时间:7/27/2023 最后编辑:Srinivas 更新时间:8/3/2023 访问量:129

问:

我得到一个带有几个特殊字符的 xml,例如这些 Pre‿charged ‿,当我解析此数据时,我无法解析。在我的 xml 中,我得到了 PrexE2x80?充电或预充电,有什么方法可以对此进行编码? 示例代码:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        Document document = null;

        try 
        {
            builder = factory.newDocumentBuilder();
            document = builder.parse(xmlFile); // xml data in string format
            document.getDocumentElement().normalize();

        } 
        catch (ParserConfigurationException e) 
        {
            System.out.printf("Failed to parse XML Feed data", e);
        }

我尝试了 UTF-8、ANSII 和其他一些编码技术。 编辑: 读取 xml 数据,如下所示

StringBuffer outputData = new StringBuffer();
        URL url;
        InputStream is = null;
        BufferedReader br;
        String line;

        try 
        {
            url = new URL(data_url);
            is = url.openStream();
            br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
            while ((line = br.readLine()) != null) 
            {
                outputData.append(line.trim());
            }
            br.close();
        } 
        catch (MalformedURLException mue) 
        {
            LOGGER.error("MalformedURLException while fetching feed data", mue);
        } 
        catch (IOException ioe) 
        {
            LOGGER.error("IOException while fetching feed data", ioe);
        } 
        finally 
        {
            try 
            {
                if (is != null) 
                {
                    is.close();
                }
            } 
            catch (IOException ioe) 
            {
                // Silent fail
            }
        }

        return outputData.toString();

当我们在浏览器中点击 URL 时,它看起来像 - Browser visibility。 将数据读入文件后,它看起来像After reading

在直接从 URL 读取数据到文件后,我没有将其转换为 String,而是获得了这样的数据Direct data

使用 Files.copy(URI.create(data_url).toURL().openStream(), Path.of(“data-copy.x”)) 函数后,我的输出如下所示testing

Java 编码 UTF-8 字符编码 XML 解析

评论

1赞 Tim Moore 7/27/2023
XML 数据如何进入 String?决定性错误可能就是在这一点上引入的。
1赞 Tim Moore 7/27/2023
我建议直接传递而不是读入第一个。这样,它将使用数据中 XML 声明中声明的编码。我怀疑它不是 UTF-8。InputStreambuilder.parseString
2赞 g00se 7/28/2023
E2 80 BF是“undertie”字符的 UTF-8 编码。您究竟是如何查看该文件的,您能否在问题中粘贴它的小屏幕截图
2赞 g00se 7/28/2023
你没有说你是怎么看的。不管它是什么,它可能将其读取为 7/8 位编码而不是 UTF-8
1赞 g00se 7/29/2023
好的,很高兴我们清除了它。请尝试以下操作。如果合适,我会将其作为答案发布:代替 outputData.append(line.trim()); 实际上,无论如何,在该上下文中,该内衣看起来像是输入错误outputData.append(line.trim().replaceAll("\u203F", "-"));

答:

0赞 Uday Kumar 8/3/2023 #1

我看不出您使用的代码有任何问题,而是问题出在您用来查看文件的客户端上,即 Notepad ++(在记事本中打开相同的文件,您会看到预收费,就像您打开谷歌驱动器链接时看到的那样)。

我已经下载了文件并检查了编码,它是 utf-8,您也使用相同的技术进行阅读。

如果您在任何其他客户端中看到预收费 例如:如果您在 eclipse 控制台中打印输出数据,您将看到 pre?charged,这可能是因为以下设置

右键单击项目 -> 属性 ->资源 -> 检查 文本文件编码 点击这里 它可能不是 UTF-8 如果不是 UTF-8,请单击其他下拉列表并选择 UTF-8 单击此处

现在重新运行您将看到预收费的方法

数据的显示会因客户端的不同而有所不同,检查客户端使用的解码类型总是好的。它应该与用于对数据进行编码的相同。

String s = "‐";
ByteBuffer buffer =StandardCharsets.UTF_8.encode(s);
System.out.println("XmlFileReader.main() "+newString(buffer.array(),StandardCharsets.UTF_16LE));
System.out.println("XmlFileReader.main() "+newString(buffer.array(),StandardCharsets.ISO_8859_1));
System.out.println("XmlFileReader.main() "+newString(buffer.array(),StandardCharsets.US_ASCII));
System.out.println("XmlFileReader.main() "+newString(buffer.array(),StandardCharsets.UTF_16));
System.out.println("XmlFileReader.main() "+newString(buffer.array(),StandardCharsets.UTF_8));

输出:

XmlFileReader.main() 胢�
XmlFileReader.main() â€
XmlFileReader.main() ���
XmlFileReader.main() �
XmlFileReader.main() ‐

即使在记事本++中,如果更改编码,您将看到预期的数据 点击这里

评论

0赞 Srinivas 8/3/2023
感谢您的详细解释,将尝试此场景
0赞 g00se 8/3/2023
就个人而言,我仍然会运行我给你的代码 - 使用 Unicode 连字符而不是“ascii 连字符”并没有真正获得任何好处。正如你所展示的,这只是一个潜在的问题来源