java 非法 XML 字符 /Axis
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/893339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
illegal XML characters /Axis
提问by
I've developped a web service and deployed it with Axis. All is running very well but I've a problem when I consume the service using a String containing a non printable character (such as ETX, FS,..). I have the following error:
我开发了一个 Web 服务并用 Axis 部署了它。一切都运行得很好,但是当我使用包含不可打印字符(例如 ETX、FS 等)的字符串使用服务时遇到了问题。我有以下错误:
exception: java.lang.IllegalArgumentException: The char '0x1c' after '....' is not a valid XML character.
Have you any ideas please?
你有什么想法吗?
edit :
编辑 :
I have to send a frame to my server using web service. My frame has a strict form( containing some non printable character as separator)
我必须使用 Web 服务将帧发送到我的服务器。我的框架有一个严格的形式(包含一些不可打印的字符作为分隔符)
class Automate {void checkFrame(String frame){// checking the frame}}
wsdl file
wsdl 文件
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:part element="impl:checkFrameResponse" name="parameters"/>
<wsdl:part element="impl:checkFrame" name="parameters"/>
<wsdl:operation name="checkFrame">
<wsdl:input message="impl:checkFrameRequest" name="checkFrameRequest"/>
<wsdl:output message="impl:checkFrameResponse" name="checkFrameResponse"/>
</wsdl:operation>
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="checkFrame">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="checkFrameRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="checkFrameResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:port binding="impl:AutomateSoapBinding" name="Automate">
<wsdlsoap:address location="http://localhost:8080/Gateway/services/Automate"/>
</wsdl:port>
回答by Jon Skeet
This is a natural problem with SOAP, unfortunately - it uses XML for text, and those characters can't be represented in XML (even with entities).
不幸的是,这是 SOAP 的一个自然问题——它使用 XML 表示文本,而这些字符不能用 XML 表示(即使是实体)。
Can you escape the non-printable characters somehow? You'll need to find some way of not representing them as straight text, unfortunately.
你能以某种方式转义不可打印的字符吗?不幸的是,您需要找到某种不将它们表示为纯文本的方法。
回答by Jim Ferrans
As you form the XML you're going to tuck inside the SOAP XML envelope, you need to make sure you don't have any unescaped characters in your attribute values and in any text nodes you have in your elements. That is:
当您形成要放入 SOAP XML 信封的 XML 时,您需要确保属性值和元素中的任何文本节点中没有任何未转义的字符。那是:
<your_elt your_attr="Don't put unescaped chars here, eg, apostrophe">
<foo>
Be sure to escape stuff here too, like: 2 < 100
A greek lambda is escaped like this: λ
</foo>
</your_elt>
I assume you're doing this in Java, so you should look into libraries that do this for you automatically. Apache has StringEscapeUtils, for instance.
我假设您是在 Java 中执行此操作,因此您应该查看自动为您执行此操作的库。例如,Apache 有StringEscapeUtils。
Your control characters would need to be escaped by XML numeric character references. Hopefully StringEscapeUtils handles that for you.
您的控制字符需要通过 XML数字字符引用进行转义。希望 StringEscapeUtils 为您处理。
Hope this helps.
希望这可以帮助。
回答by Jim Ferrans
use CDATA for data that is not part of the xml structure (i.e content), if i understand correctly and youre just routing messages this is what you should do.
将 CDATA 用于不属于 xml 结构(即内容)的数据,如果我理解正确并且您只是路由消息,这就是您应该做的。
回答by Martin Peck
It sounds like you have text that can't possibly be represented in XML. You will have to escape these characters, but to be honest I suspect you should "escape" the entire string. Base64 encoding the string might work, but you could also look at MTOM or some other mechanism of passing binary data across web services.
听起来您的文本不可能用 XML 表示。您将不得不转义这些字符,但老实说,我怀疑您应该“转义”整个字符串。Base64 编码字符串可能有效,但您也可以查看 MTOM 或其他一些跨 Web 服务传递二进制数据的机制。
If you own both sides of this system (the clients and the webservice) then it shouldn't be too difficult to add the encode/decode steps and base64 encoding should be enough.
如果您拥有这个系统的双方(客户端和网络服务),那么添加编码/解码步骤应该不会太难,base64 编码应该就足够了。

