I have a class similar to:
@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;
}
}
and I want produce an xml file similar to:
<?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>
How could I adjust my class in order to produce an XML described?
The important thing that I am asking is how can I set myObject in order to have the following example:
<myObject value="34">
<set varname="name" value="String"/>
<set varname="age" value="String"/>
</myObject>
I am using 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));
and this is the XML I have now:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myObject value="34">
<age>String</age>
<name>String</name>
</myObject>
Thanks!
you need to create a few more JaxB objects
MyObjects
MyObject
Set
To test I created an object as you described and ran it through your marshaller
the output looks like this
which is what you are after