jaxb 编组器 characterEscapeHandler
我有以下问题。我已将以下属性设置为编组器:
marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE ); marshaller.setProperty( "com.sun.xml.bind.characterEscapeHandler", new CharacterEscapeHandler() { public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException { String s = new String(ch, start, length); System.out.println("Inside CharacterEscapeHandler..."); out.write(StringEscapeUtils.escapeXml(StringEscapeUtils.unescapeXml(s))); } });
当我尝试使用以下代码将对象编组到 SOAPBody 时:
SOAPMessage message = MessageFactory.newInstance().createMessage(); marshaller.marshal(request, message.getSOAPBody());
不会调用 CharacterEscapeHandler.escape,并且不会转义字符,但此代码:
StringWriter writer = new StringWriter(); marshaller.marshal(request, writer);
调用 CharacterEscapeHandler.escape() ,并且所有字符都被转义...这是 JAXB 的正常行为吗?在将字符放入 SOAP 主体之前如何对其进行转义?
更新:
我们的系统必须与另一个系统通信,该系统期望文本被转义。 其他系统发送的消息示例:
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"> <env:Body xmlns:ac="http://www.ACORD.org/Standards/AcordMsgSvc/1"> <ac:CallRs xmlns:ac="http://www.ACORD.org/Standards/AcordMsgSvc/1"> <ac:Sender> <ac:PartyId>urn:partyId</ac:PartyId> <ac:PartyRoleCd/> <ac:PartyName>PARTYNAME</ac:PartyName> </ac:Sender> <ac:Receiver> <ac:PartyRoleCd>broker</ac:PartyRoleCd> <ac:PartyName>�марант Българи� ООД</ac:PartyName> </ac:Receiver> <ac:Application> <ac:ApplicationCd>applicationCd</ac:ApplicationCd> <ac:SchemaVersion>schemaversion/</ac:SchemaVersion> </ac:Application> <ac:TimeStamp>2011-05-11T18:41:19</ac:TimeStamp> <ac:MsgItem> <ac:MsgId>30d63016-fa7d-4410-a19a-510e43674e70</ac:MsgId> <ac:MsgTypeCd>Error</ac:MsgTypeCd> <ac:MsgStatusCd>completed</ac:MsgStatusCd> </ac:MsgItem> <ac:RqItem> <ac:MsgId>d8c2d9c4-3f1c-459f-abe1-0e9accbd176b</ac:MsgId> <ac:MsgTypeCd>RegisterPolicyRq</ac:MsgTypeCd> <ac:MsgStatusCd>completed</ac:MsgStatusCd> </ac:RqItem> <ac:WorkFolder> <ac:MsgFile> <ac:FileId>cid:28b8c9d1-9655-4727-bbb2-3107482e7f2e</ac:FileId> <ac:FileFormatCd>text/xml</ac:FileFormatCd> </ac:MsgFile> </ac:WorkFolder> </ac:CallRs> </env:Body> </env:Envelope>
所以我需要转义开始/结束标记之间的所有文本..就像 ac:PartyName 中的这样
I have the following problem. I've set the following properties to the marshaller:
marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE ); marshaller.setProperty( "com.sun.xml.bind.characterEscapeHandler", new CharacterEscapeHandler() { public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException { String s = new String(ch, start, length); System.out.println("Inside CharacterEscapeHandler..."); out.write(StringEscapeUtils.escapeXml(StringEscapeUtils.unescapeXml(s))); } });
When i try to marshall an object to SOAPBody with the following code:
SOAPMessage message = MessageFactory.newInstance().createMessage(); marshaller.marshal(request, message.getSOAPBody());
the CharacterEscapeHandler.escape is not invoked, and the characters are not escaped, but this code:
StringWriter writer = new StringWriter(); marshaller.marshal(request, writer);
invokes CharacterEscapeHandler.escape(), and all the characters are escaped... Is this normal behaviour for JAXB. And how can I escape characters before placing them inside SOAP's body?
Update:
Our system have to communicate with another system, which expects the text to be escaped.
Example for message sent by the other system:
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"> <env:Body xmlns:ac="http://www.ACORD.org/Standards/AcordMsgSvc/1"> <ac:CallRs xmlns:ac="http://www.ACORD.org/Standards/AcordMsgSvc/1"> <ac:Sender> <ac:PartyId>urn:partyId</ac:PartyId> <ac:PartyRoleCd/> <ac:PartyName>PARTYNAME</ac:PartyName> </ac:Sender> <ac:Receiver> <ac:PartyRoleCd>broker</ac:PartyRoleCd> <ac:PartyName>�марант Българи� ООД</ac:PartyName> </ac:Receiver> <ac:Application> <ac:ApplicationCd>applicationCd</ac:ApplicationCd> <ac:SchemaVersion>schemaversion/</ac:SchemaVersion> </ac:Application> <ac:TimeStamp>2011-05-11T18:41:19</ac:TimeStamp> <ac:MsgItem> <ac:MsgId>30d63016-fa7d-4410-a19a-510e43674e70</ac:MsgId> <ac:MsgTypeCd>Error</ac:MsgTypeCd> <ac:MsgStatusCd>completed</ac:MsgStatusCd> </ac:MsgItem> <ac:RqItem> <ac:MsgId>d8c2d9c4-3f1c-459f-abe1-0e9accbd176b</ac:MsgId> <ac:MsgTypeCd>RegisterPolicyRq</ac:MsgTypeCd> <ac:MsgStatusCd>completed</ac:MsgStatusCd> </ac:RqItem> <ac:WorkFolder> <ac:MsgFile> <ac:FileId>cid:28b8c9d1-9655-4727-bbb2-3107482e7f2e</ac:FileId> <ac:FileFormatCd>text/xml</ac:FileFormatCd> </ac:MsgFile> </ac:WorkFolder> </ac:CallRs> </env:Body> </env:Envelope>
So I need to escape all the text between the opening/closing tags.. like this inside ac:PartyName
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您编组到 DOM 文档时,JAXB 不负责实际的序列化和转义,它只是在内存中构建 DOM 树。然后序列化由 DOM 实现处理。
编写 xml 时需要额外的转义通常表明存在设计问题或未正确使用 xml。如果您可以提供更多背景信息为什么需要这种转义,也许我可以建议一个替代解决方案。
When you marshal to a DOM Document, JAXB is not in charge of the actual serialization and escaping, it just builds the DOM tree in memory. The serialization is then handled by the DOM implementation.
Needing additional escaping when writing xml is usually a sign of a design problem or not using xml correctly. If you can give some more context why you need this escaping, maybe I could suggest an alternative solution.