在我的代码中,我有这个 bean:
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Data implements Serializable {
@XmlAttribute
private String _name;
@XmlElementWrapper(name = "statuses")
@XmlElement(name="status")
private List<Boolean> _statuses;
public String getName() {
return _name;
}
public void setName(String name) {
_name = name;
}
public List<Boolean> getStatuses() {
return _statuses;
}
public void setStatuses(List<Boolean> statuses) {
_statuses = statuses;
}
....
}
这是编组 bean 的代码:
Writer w = null;
try {
final JAXBContext context = JAXBContext.newInstance(Data.class);
final Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
w = new FileWriter("file.xml");
m.marshal(obj, w);
} catch (Exception e) {
e.printStackTrace();
} finally {
try { w.close();}
catch (Exception e) {}
}
我期望的是:
<data name="data-test">
<statuses>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
</statuses>
...
</data>
但我得到了:
<data name="data-test">
<statuses>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
<status>true</status>
</statuses>
// strange repetition
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
<statuses>true</statuses>
// attribute as separated element
<name>data-test</name>
...
</data>
我怎样才能避免那种奇怪的重复呢?