JAXB - marshal string with xml value without the value getting escaped

I'm trying to marshal a String that contains xml.

For example:





foo

5




The result I get is always an escaped String:

<articles><article><name>foo</name><price>5</price></article></articles>

I created a test which looks like this:

public class XmlTest {

public static void main(String[] args) {

Flex flex = new Flex();

flex.setValueA("lalala");

flex.setValueB("
foo5
");

JAXBElement el = new JAXBElement(new QName("Blub"), flex.getClass(), flex);

try {

JAXBContext jaxbContext = JAXBContext.newInstance(Flex.class);

Marshaller marshaller = jaxbContext.createMarshaller();

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

marshaller.marshal(flex, System.out);

JAXBContext jaxbContext2 = JAXBContext.newInstance(Flex.class);

Marshaller marshaller2 = jaxbContext2.createMarshaller();

marshaller2.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

marshaller2.marshal(el, System.out);

} catch (JAXBException e) {

System.out.println(e);

}

}

@XmlRootElement(name = "foo")

@XmlAccessorType(XmlAccessType.FIELD)

public static class Flex extends AuditableVo {

@XmlElement(name = "valueA")

@Getter

@Setter

private String valueA;

@XmlElement(name = "valueB")

@Getter

@Setter

private String valueB;

}

}

I've tried a lot of suggestions from stackoverflow, but...

Read More »

By: StackOverFlow - Monday, 16 April

Related Posts