@XmlRootElement
public class Container{
private String name;
private String value;
public Container(String name, String value){
this.name = name;
this.value = value;
}
}
使用编组:
公共课演示{
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Container.class);
Container container = new Container("potatoes","5");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(container, System.out);
}
}
我收到的输出是:
<Container>
<name>potatoes</name>
<value>5</value>
</Container>
是否有任何输出方式如下:
<Container>
<potatoes>5</potatoes>
</Container>
使用JAXB?
答案 0 :(得分:3)
您可以使用@ XmlAnyElement-annotated属性并将返回元素用作JAXBElement:
@XmlAnyElement
public JAXBElement<String> getThing() {
return new JAXBElement<String>(new QName(this.name), String.class, this.value);
}