不幸的是,在编组后,一些元素是空的并且想要被序列化。
编组:
public static <T> String marschallObjectToXml(final Class<T> clazz, T object) {
try {
JAXBContext context = JAXBContext.newInstance(clazz);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, UTF_8);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
QName qName = new QName(clazz.getPackage().getName(), clazz.getSimpleName());
Object objectToMarshal = new JAXBElement<T>(qName, clazz, object);
// object to XML
StringWriter sw = new StringWriter();
marshaller.marshal(objectToMarshal, sw);
return sw.toString();
} catch (JAXBException e) {
throw new RuntimeException(FEHLER_BEIM_PARSEN + e.getMessage(), e);
}
}
对象1:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"personen", "kinder"})
public static class PersonenUndBeziehungen {
@XmlElement(name = "Personen", required = true)
protected BqDatenTyp.PersonenUndBeziehungen.Personen personen;
// ...
对象2:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"person"})
public static class Personen {
@XmlElement(name = "Person", required = true)
protected List<PersonTyp> person;
public List<PersonTyp> getPerson() {
if (person == null) {
person = new ArrayList<PersonTyp>();
}
return this.person;
}
// ...
对象3:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "personTyp", propOrder = {"lfdID", "kundennummern", "typ", "vertriebsmassnahmen", "liquiditaet",
"risikobereitschaft", "kenntnisseUndErfahrungen", "bankmitarbeiter", "mitglied", "natuerlichePerson",
"juristischePerson", "betreuung", "beziehungen", "vermoegenswerte"})
public class PersonTyp {
@XmlElement(name = "LfdID", required = true)
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
@XmlSchemaType(name = "normalizedString")
protected String lfdID;
@XmlElement(name = "Kundennummern", required = true)
protected PersonTyp.Kundennummern kundennummern;
@XmlElement(name = "Typ", required = true)
@XmlSchemaType(name = "normalizedString")
// ...
如果我在调试模式下启动程序,我可以看到,List:T Object - &gt; PersonenUndBeziehungen - &gt; Personen - &gt;人将被一个对象填充,并且它不是空的。但在序列化后我看到:
<personenUndBeziehungen>
<kinder/>
<personen/> <!-- THIS SHOULD NOT BE EMPTY. IS EMPTY??? -->
</personenUndBeziehungen>
任何想法!? 非常感谢提前! PS:Java 1.7