我无法理解我在做什么,我需要解组如下所示的xml文件:
<ApplicationMetadata xmlns="http://www.sas.com/xml/schema/namespace/ApplicationMetadata-9.4">
<Role Name="name1" Desc="desc1 " DisplayName="disp1 ">
<Members/>
<ContributingRoles/>
<Capabilities>
<Capability CapabilityId="1"/>
<Capability CapabilityId="2"/>
<Capability CapabilityId="3"/>
</Capabilities>
</Role>
<Role Name="name2" Desc="desc2" DisplayName="disp2">
<Members>
<UserGroup Name="userGoup"/>
</Members>
<ContributingRoles/>
<Capabilities>
<Capability CapabilityId="1"/>
<Capability CapabilityId="2"/>
</Capabilities>
</Role>
</ApplicationMetadata>
我上下一课:
@XmlRootElement(name = "ApplicationMetadata", namespace = "http://www.sas.com/xml/schema/namespace/ApplicationMetadata-9.4")
@XmlAccessorType(XmlAccessType.FIELD)
public class ApplicationMetaData {
@XmlElement(name = "Role")
private List<Role> roles;
getters and setters
}
@XmlRootElement(name = "Role")
@XmlAccessorType(XmlAccessType.FIELD)
public class Role {
@XmlAttribute(name = "Name")
private String name;
@XmlAttribute(name = "Desc")
private String desc;
@XmlAttribute(name = "DisplayName")
private String displayName;
@XmlElementWrapper(name = "ContributingRoles")
@XmlElement(name = "UserGroup")
private List<UserGroup> contributionRoles;
@XmlElementWrapper(name = "Members")
@XmlElement(name = "UserGroup")
private List<UserGroup> members;
@XmlElementWrapper(name = "Capabilities")
@XmlElement(name = "Capability")
private List<Capability> capabilities;
getters and setters
}
@XmlRootElement(name = "Capability")
@XmlAccessorType(XmlAccessType.FIELD)
public class Capability {
@XmlAttribute(name = "CapabilityId")
private String id;
getters and setters
}
@XmlRootElement (name = "UserGroup")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserGroup {
@XmlAttribute(name = "Name")
private String name;
getters and setters
}
我的编组代码是:
File file = new File(fileName);
ApplicationMetaData appMetaData = (ApplicationMetaData)
WorkWithXml.unmarshalXml(file, ApplicationMetaData.class);
public static Object unmarshalXml(File file, Class unmarshallerClass) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(unmarshallerClass);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
return unmarshaller.unmarshal(file);
} catch (JAXBException e) {
LOG.error("", e);
throw new AutotestError(e);
}
}
结果是我有appMetaData对象,它具有文件中的所有角色。角色具有属性,但角色内的所有列表均为空。不为空,但为空。我哪里出错了?
P.S。一切都与Java代码有关:)
答案 0 :(得分:0)
我已经完成了:)
相反
@XmlElementWrapper(name = "Capabilities")
@XmlElement(name = "Capability")
private List<Capability> capabilities;
在我使用过的角色类中
@XmlElement (name = "Capabilities")
Capabilities capabilities;
这里是功能类
@XmlRootElement(name = "Capabilities")
@XmlAccessorType(XmlAccessType.FIELD)
public class Capabilities {
@XmlElement(name = "Capability")
private List<Capability> capability;
public List<Capability> getCapability() {
return capability;
}
public void setCapability(List<Capability> capability) {
this.capability = capability;
}
}