【发布时间】:2014-05-23 20:23:08
【问题描述】:
你的问题很简单,但我没有发现其他人有同样的问题。 我正在尝试使用 JAXB 创建 XML,所以 我有课:
@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?
【问题讨论】:
-
你想要的输出...potatoes = 5 and value = 5。(5正在重复)。这没有多大意义。一个标签通常会在其中包含一些信息....可能是您需要重新考虑您的需求/设计,或者您可能给出了错误的示例。
-
这是一个例子。我当然想做一些更有意义的事情。不过我调整了这个例子:)