好,所以基本上你的Java對象結構是錯誤的。您需要分析XML,然後您需要相應地構建Java對象結構。
如果你看看XML,你有一個文檔,在文檔中你有屬性,內部屬性的基本屬性等等。
所以同樣你需要有java類,文檔類,文檔內你需要有屬性類,在基類內等等。我已經在下面展示了這個類的結構,這樣你就可以得到一個想法。
文檔類 -
@XmlRootElement(name = "document")
@XmlAccessorType(XmlAccessType.FIELD)
public class PDFDocument {
@XmlElement(name = "properties")
private DocumentProperty documentProperty;
public DocumentProperty getDocumentProperty() {
return documentProperty;
}
public void setDocumentProperty(DocumentProperty documentProperty) {
this.documentProperty = documentProperty;
}
@Override
public String toString() {
return "PDFDocument{" +
"documentProperty=" + documentProperty + "\n" +
'}';
}
}
類來保存屬性 -
@XmlRootElement(name = "properties")
@XmlAccessorType(XmlAccessType.FIELD)
public class DocumentProperty {
@XmlElement(name = "basic")
private Basic basic;
public Basic getBasic() {
return basic;
}
public void setBasic(Basic basic) {
this.basic = basic;
}
@Override
public String toString() {
return "DocumentProperty{" +
"basic=" + basic + "\n" +
'}';
}
}
基本類 -
@XmlRootElement(name = "basic")
@XmlAccessorType(XmlAccessType.FIELD)
public class Basic {
@XmlElementRef
private List<Property> propertyList;
public List<Property> getPropertyList() {
return propertyList;
}
public void setPropertyList(List<Property> propertyList) {
this.propertyList = propertyList;
}
@Override
public String toString() {
return "Basic{" +
"propertyList=" + propertyList + "\n" +
'}';
}
}
地產類 -
@XmlRootElement(name = "property")
@XmlAccessorType(XmlAccessType.FIELD)
public class Property {
@XmlAttribute(name = "id")
private String id;
@XmlElement(name = "value")
private String value;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "Property{" +
"id='" + id + "\n" +
", value='" + value + "\n" +
'}';
}
}
Above Class結構適用於您提供的XML。但我想你將不得不改變這種結構,因爲除了基本類型之外,還有其他類型的文檔中的屬性。在這種情況下,我建議你具有抽象的Document Property類,並將其擴展到Basic和其他類型的Properties。
測試類 -
public class XmlTest {
@Test
public void testXml() throws Exception {
String xml = "<document>" +
" <properties>" +
" <basic>" +
" <property id=\"generationDate\">" +
" <value>20150525</value>" +
" </property>\n" +
" <property id=\"hostAddress\">" +
" <value>192.168.0.250</value>" +
" </property>" +
" </basic>" +
" </properties>" +
"</document>";
try {
JAXBContext jaxbContext = JAXBContext.newInstance(PDFDocument.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
PDFDocument document = (PDFDocument) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(xml.getBytes()));
System.out.println("PDF Document Structure -" +document.toString());
for(Property property : document.getDocumentProperty().getBasic().getPropertyList()) {
if(property.getId().equals("generationDate")){
System.out.println("Generation Date : "+property.getValue());
}
}
} catch (JAXBException ex) {
ex.printStackTrace();
}
}
}
運行測試類產生以下結果。
PDFDocument{
documentProperty=
DocumentProperty{
basic=Basic{
propertyList=[
Property{
id='generationDate
, value='20150525
},
Property{id='hostAddress
, value='192.168.0.250
}]
}
}
}
希望對您有所幫助。
太棒了,不知道有關班級結構,所以你的答案有幫助。它現在被實現並且像一個魅力一樣工作;-) –