I've been trying to unmarshall a self-created xml configuration file to an object with JAXB default implementation, I ignore why, but some inner elements and values are skipped giving me null results.
Here is the xml document :
<import-sources>
<domain-objects>
<domain-object class="xxx.xxx.core.business.mapping.Make">
<sources>
<source name="xxxxxx" class="xxx.xxx.core.web.common.model.xxxxxxx.output.MakeDTO">
<uri secured="true" value="xxxxxxxxxxxxxxxxxx"/>
<key name="api_key" mode="in_uri" value="xxxxxxxxxxxx"/>
</source>
</sources>
</domain-object>
</domain-objects>
this is the mapped class :
@XmlRootElement(name = "import-sources")
public class ImportSources {
@XmlType
public static class DomainObject {
@XmlType
public static class Source {
@XmlType
public static class URI {
private String value;
private boolean secured;
}
@XmlType
public static class Key {
@XmlEnum
public enum Mode {
@XmlEnumValue("in_uri")
IN_URI,
@XmlEnumValue("in_header")
IN_HEADER
}
private String name;
private String value;
private Mode mode;
}
private String name;
private URI uri;
private Key key;
@XmlElement(name = "class")
private Class outputClass;
}
@XmlAttribute(name = "class")
private Class<? extends Entity> subjectClass;
@XmlElementWrapper
@XmlElement(name = "source")
private Source[] sources;
}
@XmlElementWrapper(name = "domain-objects")
@XmlElement(name = "domain-object")
private DomainObject[] domainObjects;
}
After parsing the document with Unmarshaller.unmarshall() all Source's fields are null, I can get its scalar fields if I replace its inner tag properties like so :
<source>
<name>....</name>
<class>...</class>
</source>
Which I want to avoid because of the redundancy.
我终于通过用'@XmlAttribute'注释打开标签或单个标签内的所有字段来解决我的问题。
I suggest to explicitly annotate fields as @XmlElement
or @XmlAttribute
correspondingly. Your class will look like the following then, and fields will get populated:
@XmlRootElement(name = "import-sources")
public class ImportSources {
@XmlType
public static class DomainObject {
@XmlType
public static class Source {
@XmlType
public static class URI {
@XmlAttribute //here
private String value;
@XmlAttribute //here
private boolean secured;
}
@XmlType
public static class Key {
@XmlEnum
public enum Mode {
@XmlEnumValue("in_uri")
IN_URI,
@XmlEnumValue("in_header")
IN_HEADER
}
@XmlAttribute //here
private String name;
@XmlAttribute //here
private String value;
@XmlAttribute //here
private Mode mode;
}
@XmlAttribute //here
private String name;
@XmlElement //here
private URI uri;
@XmlElement //here
private Key key;
@XmlElement(name = "class")
private Class outputClass;
}
@XmlAttribute(name = "class")
private Class<? extends Entity> subjectClass;
@XmlElementWrapper
@XmlElement(name = "source")
private Source[] sources;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.