首页javajaxbJava HTML/XML - 如何绑定JAXB多行属性

Java HTML/XML - 如何绑定JAXB多行属性

我们想知道如何绑定JAXB多行属性。
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;
    }
  }
}