关于 Java 上无效 XML 字符的错误 [英] Error about invalid XML characters on Java

查看:28
本文介绍了关于 Java 上无效 XML 字符的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 上解析 xml 文件时出现错误:

Parsing an xml file on Java I get the error:

在文档的元素内容中发现无效的 XML 字符(Unicode:0x0).

xml 来自网络服务.

The xml comes from a webservice.

问题是只有当 webservice 在 localhost (windows+tomcat) 上运行时才会出现错误,而当 webservice 在线时 (linux+tomcat) 不会出现错误.

The problem is that I get the error only when the webservice is running on localhost (windows+tomcat), but not when the webservice is online (linux+tomcat).

如何替换无效字符??谢谢.

How can I replace the invalid char?? Thanks.

推荐答案

用这个代码修复:

String cleanXMLString = null;
Pattern pattern = null;
Matcher matcher = null;
pattern = Pattern.compile("[\000]*");
matcher = pattern.matcher(dirtyXMLString);
if (matcher.find()) {
   cleanXMLString = matcher.replaceAll("");
}

这篇关于关于 Java 上无效 XML 字符的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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