XML:JAXB Unmarshals Nested Yếu tố để Null
<datasource formatted-name="blah" inline="blah">
<repository-location derived-from="blah" id="blah" path="blah" revision="blah" site="blah"/>
</datasource>
Tôi cố gắng để unmarshal tất cả mọi thứ dưới một lớp (DataSource) với các lớp tĩnh lồng nhau. Đây là lớp DataSource của tôi:
@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);
tôi có thể lĩnh vực DataSource đầu ra thành công "formattedName" và "nội tuyến", nhưng "repositoryLocation" là null. Ai đó có thể giúp đỡ được không?
cần làm việc. Bạn có getters/setters cho 'repositoryLocation'? – ulab
vâng, tôi có chúng. – toadead
làm thế nào để bạn unmarshall? xin thêm mã. – ulab