我的课程类似于:
@XmlRootElement
public class myObject{
String name;
String age;
String value;
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
@XmlElement
public void setAge(String age) {
this.age = age;
}
public String getValue() {
return value;
}
@XmlAttribute
public void setValue(String value) {
this.value = value;
}
}
我希望生成一个类似于:
的xml文件<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myObjects>
<myObject value="34">
<set varname="name" value="String"/>
<set varname="age" value="String"/>
</myObject>
<myObject value="345">
<set varname="name" value="String"/>
<set varname="age" value="String"/>
</myObject>
</myObjects>
如何调整我的类以生成所描述的XML?
我要问的重要一点是如何设置myObject以获得以下示例:
<myObject value="34">
<set varname="name" value="String"/>
<set varname="age" value="String"/>
</myObject>
我正在使用JAXB:
myObject customer = new myObject();
customer.setValue("100");
customer.setName("mkyong");
customer.setAge("29");
....fill my object here
JAXBContext jaxbContext = JAXBContext.newInstance(myObject.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter writer = new StringWriter();
jaxbMarshaller.marshal(customer, System.out);
jaxbMarshaller.marshal(customer, new StreamResult(writer));
这就是我现在拥有的XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myObject value="34">
<age>String</age>
<name>String</name>
</myObject>
谢谢!
答案 0 :(得分:1)
您需要再创建一些JaxB对象
<强> MyObjects 强>
@XmlRootElement(name="myObjects")
public class MyObjects {
protected List<MyObject> myObject;
public MyObjects(){
setMyObject(new ArrayList<MyObject>());
}
public List<MyObject> getMyObject() {
return this.myObject;
}
@XmlElement(name="myObject")
public void setMyObject(List<MyObject> myObject) {
this.myObject = myObject;
}
}
<强>为MyObject 强>
public class MyObject {
String value;
List<Set> set;
public MyObject(){
set = new ArrayList<Set>();
}
public List<Set> getSet() {
return this.set;
}
@XmlElement
public void setSet(List<Set> set) {
this.set = set;
}
public String getValue() {
return value;
}
@XmlAttribute
public void setValue(String value) {
this.value = value;
}
}
设置强>
@XmlType(propOrder = {
"varName",
"value"
})
public class Set {
String varName;
String value;
public String getVarName() {
return varName;
}
@XmlAttribute
public void setVarName(String varName) {
this.varName = varName;
}
public String getValue() {
return value;
}
@XmlAttribute
public void setValue(String value) {
this.value = value;
}
}
测试我创建了一个你描述的对象并通过你的编组器
运行它MyObjects mos = new MyObjects();
MyObject mo1 = new MyObject();
mo1.setValue("34");
Set set1 = new Set();
set1.setVarName("age");
set1.setValue("String");
mo1.getSet().add(set1);
Set set2 = new Set();
set2.setVarName("name");
set2.setValue("String");
mo1.getSet().add(set2);
mos.getMyObject().add(mo1);
MyObject mo2 = new MyObject();
mo2.setValue("345");
Set set3 = new Set();
set3.setVarName("age");
set3.setValue("String");
mo2.getSet().add(set3);
Set set4 = new Set();
set4.setVarName("name");
set4.setValue("String");
mo2.getSet().add(set4);
mos.getMyObject().add(mo2);
输出看起来像这样
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myObjects>
<myObject value="34">
<set varName="age" value="String"/>
<set varName="name" value="String"/>
</myObject>
<myObject value="345">
<set varName="age" value="String"/>
<set varName="name" value="String"/>
</myObject>
</myObjects>
这就是你的目标