Reputation: 5013
I am trying to output the following XML using JAXB:
<ScreenData step="1" description="My descriotion">
<element name="name1" type="type1" value="value1"/>
<element name="name2" type="type2" value="value2"/>
</ScreenData>
To do this I'm using the following code:
screenData.getElement().add(element);
element.setName("name1");
element.setType("type1");
element.setValueAttribute("value1");
screenData.getElement().add(element);
element.setName("name2");
element.setType("type2");
element.setValueAttribute("value2");
This is then what is output:
<ScreenData step="1" description="My First XML">
<element name="name2" type="type2" value="value2"/>
<element name="name2" type="type2" value="value2"/>
</ScreenData>
Upvotes: 3
Views: 1425
Reputation: 149017
You need to ensure that you are creating separate instances of Element
. Currently you appear to be adding the same instance twice.
Element element1 = new Element();
screenData.getElement().add(element1);
element1.setName("name1");
element1.setType("type1");
element1.setValueAttribute("value1");
Element element2 = new Element();
screenData.getElement().add(element2);
element2.setName("name2");
element2.setType("type2");
element2.setValueAttribute("value2");
For More Information
Upvotes: 3
Reputation: 5145
You have to create a Set or a List.
List<MyClass> l= new ArrayList<MyClass>();
myClass = new MyClass();
myClass.setAttr("attr1");
l.add(myClass);
myClass2 = new MyClass();
myClass2.setAttr("attr2");
l.add(myClass2);
Upvotes: 0