您不需要为您的系统中的各种概要文件类型编写自定义适配器
ProfileSet
相反,要处理这种混合的XML内容,规范方法是这样的。
轮廓集
类,您应该定义多态Java属性
profile
<organization.information-profile>
,
<connection-profile>
或
<user-information-profile>
(我更喜欢这个名字
轮廓
innerPayload
这些XML元素名和Java类之间的映射已经完成
通过使用
@XmlElements
注释。
@XmlAccessorType(XmlAccessType.FIELD)
public class ProfileSet {
@XmlElement(name = "name")
private String name;
// template to store different profile objects
@XmlElements({
@XmlElement(name = "organization-information-profile", type = OrganizationInfomationProfile.class),
@XmlElement(name = "connection-profile", type = ConnectionProfile.class),
@XmlElement(name = "user-information-profile", type = UserInformationProfile.class)
})
private Profile profile;
// default constructor used by JAXB unmarshaller
public ProfileSet() {
}
public ProfileSet(String name, Profile profile) {
this.name = name;
this.profile = profile;
}
}
你需要一个抽象的超类
Profile
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class Profile {
@XmlElement
private String name;
@XmlElement(name = "attribute")
private List<Attribute> attributes;
}
你有一个子类
OrganizationInformationProfile
代表
<organization-information-profile>
要素
@XmlAccessorType(XmlAccessType.FIELD)
public class OrganizationInfomationProfile extends Profile {
@XmlElement(name = "qualified-name")
private String qualifiedName;
@XmlElement(name = "last-name")
private String lastName;
@XmlElement(name = "address")
private String address;
// ... other properties
}
ConnectionProfile
代表
<连接配置文件>
要素
@XmlAccessorType(XmlAccessType.FIELD)
public class ConnectionProfile extends Profile {
@XmlElement(name = "service")
private Service service;
}
还有另一个亚类
UserInformationProfile
代表
<用户信息配置文件>
元素。
通过使用上述方法,您可以解组XML示例