import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; public class Main { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Foo.class); Foo foo = new Foo(); foo.setBar("Hello\nWorld"); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(foo, System.out); } @XmlRootElement public static class Foo { private String bar; @XmlAttribute public String getBar() { return bar; } public void setBar(String bar) { this.bar = bar; } } }