Can I force JAXB not to convert " into ", for example, when marshalling to XML?

后端 未结 14 1972
情深已故
情深已故 2020-11-29 09:27

I have an Object that is being marshalled to XML using JAXB. One element contains a String that includes quotes (\"). The resulting XML has " where t

14条回答
  •  独厮守ぢ
    2020-11-29 09:54

    The simplest way, when using sun's Marshaller implementation is to provide your own implementation of the CharacterEscapeEncoder which does not escape anything.

        Marshaller m = jcb.createMarshaller();
    m.setProperty(
        "com.sun.xml.bind.marshaller.CharacterEscapeHandler",
        new NullCharacterEscapeHandler());
    

    With

    public class NullCharacterEscapeHandler implements CharacterEscapeHandler {
    
        public NullCharacterEscapeHandler() {
            super();
        }
    
    
        public void escape(char[] ch, int start, int length, boolean isAttVal, Writer writer) throws IOException {
            writer.write( ch, start, length );
        }
    }
    

提交回复
热议问题