且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用声明的encoding = utf-8 - Java从xml中删除非UTF-8字符

更新时间:2023-11-25 08:24:52


1)我得到xml作为java字符串与其中(我现在没有访问接口,但我可能得到xml作为一个java串)。我可以用replaceAll(£,)去掉这个字符吗?


我假设你的意思是想要摆脱非 ASCII 字符,因为你在谈论一个遗产方面。您可以使用以下正则表达式来摆脱可打印ASCII范围之外的任何内容:

  string = string.replaceAll([^ \\\x20-\\\x7e],); 




2)我将xml作为字节数组 - 如何处理在这种情况下,这个操作是安全的吗?


你需要包装 byte [] ByteArrayInputStream ,以便您可以使用 InputStreamReader ,其中您指定编码,然后使用 BufferedReader 逐行阅读。



例如

  BufferedReader reader = null; 
try {
reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes),UTF-8));
for(String line;(line = reader.readLine())!= null;){
line = line.replaceAll([^ \\\x20-\\x7e] ,);
// ...
}
// ...


I have to handle this scenario in Java:

I'm getting a request in XML form from a client with declared encoding=utf-8. Unfortunately it may contain not utf-8 characters and there is a requirement to remove these characters from the xml on my side (legacy).

Let's consider an example where this invalid XML contains £ (pound).

1) I get xml as java String with £ in it (I don't have access to interface right now, but I probably get xml as a java String). Can I use replaceAll(£, "") to get rid of this character? Any potential issues?

2) I get xml as an array of bytes - how to handle this operation safely in that case?

1) I get xml as java String with £ in it (I don't have access to interface right now, but I probably get xml as a java String). Can I use replaceAll(£, "") to get rid of this character?

I am assuming that you rather mean that you want to get rid of non-ASCII characters, because you're talking about a "legacy" side. You can get rid of anything outside the printable ASCII range using the following regex:

string = string.replaceAll("[^\\x20-\\x7e]", "");

2) I get xml as an array of bytes - how to handle this operation safely in that case?

You need to wrap the byte[] in an ByteArrayInputStream, so that you can read them in an UTF-8 encoded character stream using InputStreamReader wherein you specify the encoding and then use a BufferedReader to read it line by line.

E.g.

BufferedReader reader = null;
try {
    reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes), "UTF-8"));
    for (String line; (line = reader.readLine()) != null;) {
        line = line.replaceAll("[^\\x20-\\x7e]", "");
        // ...
    }
    // ...