XML片段:JAXB解組嵌套的元素設置爲null
<datasource formatted-name="blah" inline="blah">
<repository-location derived-from="blah" id="blah" path="blah" revision="blah" site="blah"/>
</datasource>
我想一個類(數據源)與嵌套靜態類下解組的一切。這裏是我的DataSource類:
@XmlRootElement(name = "datasource")
@XmlAccessorType(XmlAccessType.FIELD)
public class DataSource {
@XmlAttribute(name = "formatted-name")
protected String formattedName;
@XmlAttribute(name = "inline")
protected String inline;
@XmlElement(name = "repository-location")
protected RepositoryLocation repositoryLocation;
// public getters and setters for fields above
@XmlAccessorType(XmlAccessType.FIELD)
public static class RepositoryLocation {
@XmlAttribute(name = "derived-from")
protected String derivedFrom;
@XmlAttribute(name = "id")
protected String id;
@XmlAttribute(name = "path")
protected String path;
@XmlAttribute(name = "revision")
protected String revision;
@XmlAttribute(name = "site")
protected String site;
// public getters and setters for fields above
}
}
的Unmarshaller:
JAXBContext jaxbContext = JAXBContext.newInstance(DataSource.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(responseXML);
dataSourceResponse = (DataSource) unmarshaller.unmarshal(reader);
我可以成功輸出數據源田 「formattedName」 和 「內聯」,但 「repositoryLocation」 爲空。有人可以幫忙嗎?
它應該工作。 ''repositoryLocation'有getter/setter嗎? – ulab
是的,我確實有他們。 – toadead
你怎麼解開?請添加代碼。 – ulab