JAXB - how to create xml from object with <set varname = "String" value = name "String2"> elements

120 Views Asked by At

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!

1

There are 1 best solutions below

0
On BEST ANSWER

you need to create a few more JaxB objects

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;
    }
}

Set

@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;
    }
}

To test I created an object as you described and ran it through your marshaller

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);

the output looks like this

<?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>

which is what you are after