How to marshall a string using JAXB that sometimes contains XML content and sometimes does not?

后端 未结 3 1819
小蘑菇
小蘑菇 2020-12-04 02:47

Consider this example -

I have a class called Report that has a field of type Message. The Message class has a field called \"body\" which is a string. \"body\" can

3条回答
  •  臣服心动
    2020-12-04 02:58

    If its only for Marshalling, and to ignore the < and >, We can use the following:

    marshaller.setProperty("com.sun.xml.bind.marshaller.CharacterEscapeHandler",
                    new CharacterEscapeHandler() {
                        @Override
                        public void escape(char[] ac, int i, int j, boolean flag,
                                Writer writer) throws IOException {
                            writer.write(ac, i, j);
                        }
                    });
    

提交回复
热议问题