我正在尝试使用JAXB输出以下XML:
<ScreenData step="1" description="My descriotion">
<element name="name1" type="type1" value="value1"/>
<element name="name2" type="type2" value="value2"/>
</ScreenData>
为此,我使用以下代码:
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");
这就是输出:
<ScreenData step="1" description="My First XML">
<element name="name2" type="type2" value="value2"/>
<element name="name2" type="type2" value="value2"/>
</ScreenData>
解决方法:
您需要确保要创建单独的Element实例.当前,您似乎要添加两次相同的实例.
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");
欲获得更多信息
> http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html