簡體   English   中英

jaxb marshaller characterEscapeHandler

[英]jaxb marshaller characterEscapeHandler

我有以下問題。 我已經為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)));
    }
});

當我嘗試使用以下代碼將對象編組到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。 如果您可以提供更多上下文,為什么需要這種轉義,也許我可以建議一個替代解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM