Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jaxb marshaller characterEscapeHandler

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>&#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>

So I need to escape all the text between the opening/closing tags.. like this inside ac:PartyName

like image 749
Georgi Georgiev Avatar asked Jan 29 '26 00:01

Georgi Georgiev


1 Answers

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.

like image 187
Jörn Horstmann Avatar answered Jan 30 '26 16:01

Jörn Horstmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!