jaxb 编组器 characterEscapeHandler

发布于 2024-11-05 17:56:04 字数 3352 浏览 0 评论 0原文

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

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 中的这样

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掐死时间 2024-11-12 17:56:04

当您编组到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文