本文介绍了使用CXF时如何处理WS输出中的无效字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring,CXF和Hibernate构建一个WebService,它对我有只读访问权的外部数据库执行搜索查询。

I'm using Spring, CXF and Hibernate to build a WebService that perform search queries on a foreign database that I have read-only access.

问题是数据库中的某些条目在文本字段中有奇怪的字符(0x2),并且它似乎用于处理/序列化从Hibernate会话返回的对象的CXF或库(Aegis?)无法处理它:

The problem is that some entries in the database have strange characters (0x2) in text fields, and it seems that CXF or the library (Aegis?) that it uses to process/serialize the objects returned from the Hibernate session can't deal with it:

org.apache.cxf.aegis.DatabindingException: Error writing document.. Nested exception is com.ctc.wstx.exc.WstxIOException: Invalid white space character (0x2) in text to output (in xml 1.1, could output as a character entity)

我如何解决这个问题?
理想情况下,我可以删除这些字符,因为它们对我的输出无关...
谢谢!

How do I get around that?Ideally, I could just remove those characters, since they don't matter for my output...Thanks!

推荐答案

/**
* From xml spec valid chars:<br>
* #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]<br>
* any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.<br>
* @param text The String to clean
* @param replacement The string to be substituted for each match
* @return The resulting String
*/
public static String CleanInvalidXmlChars(String text, String replacement) {
    String re = "[^\\x09\\x0A\\x0D\\x20-\\xD7FF\\xE000-\\xFFFD\\x10000-x10FFFF]";
    return text.replaceAll(re, replacement);
}

来源:

这篇关于使用CXF时如何处理WS输出中的无效字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 04:00