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

查看:125
本文介绍了有关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).

怎么能我替换无效的char?
谢谢。

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天全站免登陆