java - jaxb 编码器 characterEscapeHandler

标签 java jaxb marshalling

我有以下问题。我已将以下属性设置为编码器:


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>&#208;&#65533;&#208;&#188;&#208;&#176;&#209;&#8364;&#208;&#176;&#208;&#189;&#209;&#8218; &#208;&#8216;&#209;&#352;&#208;&#187;&#208;&#179;&#208;&#176;&#209;&#8364;&#208;&#184;&#209;&#65533; &#208;&#382;&#208;&#382;&#208;&#8221;</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 中一样

最佳答案

当您编码到 DOM 文档时,JAXB 不负责实际的序列化和转义,它只是在内存中构建 DOM 树。然后由 DOM 实现处理序列化。

在编写 xml 时需要额外的转义通常是设计问题或未正确使用 xml 的标志。如果您能提供更多背景信息说明为什么需要这种转义,也许我可以建议一个替代解决方案。

关于java - jaxb 编码器 characterEscapeHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5935371/

相关文章:

java - 从应用程序内启动 Android 应用程序

java - 使用其他类的字段动态创建类

java - 映射、绑定(bind)和解析之间有什么区别?

java - JAXB,CXF : There's no ObjectFactory with an @XmlElementDecl for the element . ..与

java - 如何通过 jaxb 从生成的 XML 中删除不需要的元素

java - 通过 Streams 并行执行多个查询

java - 使用 JaxB 解码包含 List<String> 字段的对象

c# - PInvoke DllExport : structure marshaling failure

.net - 在 C# 中调用 C 函数并获取字符串输出

java - 安装 Gradle Eclipse 插件后,Gradle 构建文件夹未显示