我一直试图将自创建的xml配置文件解锁到具有JAXB默认实现的对象,我忽略了原因,但是一些内部元素和值被跳过,给出了空结果。
以下是xml文档:
<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>
这是映射的类:
@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;
}使用Unmarshaller.unmarshall()解析文档后,如果替换它的内部标记属性(如:
<source>
<name>....</name>
<class>...</class>
</source>这是我想避免的,因为这是多余的。
发布于 2017-08-25 16:40:43
我终于解决了我的问题,用@XmlAttribute在开始标记或单个标记中注释了我想要的所有字段。
发布于 2017-08-25 16:46:10
我建议相应地将字段显式注释为@XmlElement或@XmlAttribute。然后,您的类将如下所示,并且字段将被填充:
@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;
}
}https://stackoverflow.com/questions/45885424
复制相似问题