你的问题很简单,但我没有发现其他人有同样的问题。 我正在尝试使用 JAXB 创建 XML,所以 我有课:
@XmlRootElement
public class Container{
private String name;
private String value;
public Container(String name, String value){
this.name = name;
this.value = value;
}
}
使用编码(marshal):
公开课演示{
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?
最佳答案
您可以使用 @XmlAnyElement 注释的属性并将元素返回为 JAXBElement:
@XmlAnyElement
public JAXBElement<String> getThing() {
return new JAXBElement<String>(new QName(this.name), String.class, this.value);
}
关于java - JAXB 使用变量值作为 XML 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23837815/