使用 JAXB 解组 xml 项的复杂嵌套列表
Unmarshalling complex nested list of xml items using JAXB
我正在尝试解组这个复杂的 xml,但未能成功。下面是我的 xml:
<ImportSession xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Batches>
<Batch BatchClassName="BC">
<BatchFields>
<BatchField Name="N11" Value="N21"/>
<BatchField Name="N12" Value="N22"/>
<BatchField Name="N13" Value="N23"/>
<BatchField Name="N14" Value="N24"/>
<BatchField Name="N15" Value="N25"/>
</BatchFields>
<Documents>
<Document FormTypeName="F1">
<IndexFields>
<IndexField Name="NM11" Value="V11"/>
<IndexField Name="NM12" Value="V12"/>
<IndexField Name="NM13" Value="V13"/>
<IndexField Name="NM14" Value="V14"/>
</IndexFields>
<Pages>
<Page ImportFileName="P1.pdf"/>
</Pages>
</Document>
<Document FormTypeName="F2">
<IndexFields>
<IndexField Name="NM21" Value="V21"/>
<IndexField Name="NM22" Value="V22"/>
<IndexField Name="NM23" Value="V23"/>
<IndexField Name="NM24" Value="V24"/>
</IndexFields>
<Pages>
<Page ImportFileName="P2.pdf"/>
</Pages>
</Document>
<Document FormTypeName="F3">
<IndexFields>
<IndexField Name="NM31" Value="V31"/>
<IndexField Name="NM32" Value="V32"/>
<IndexField Name="NM33" Value="V33"/>
<IndexField Name="NM34" Value="V34"/>
</IndexFields>
<Pages>
<Page ImportFileName="P3.pdf"/>
</Pages>
</Document>
</Documents>
</Batch>
</Batches>
</ImportSession>
这是我的ImportSession.java
import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.*;
import lombok.Getter;
import lombok.Setter;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.ToString;
@XmlRootElement(name = "ImportSession")
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public class ImportSession {
@XmlElement(name = "Batches")
private Batches batches;
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public static class Batches implements Serializable {
@XmlElement(name = "Batch")
private Batch batch;
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public static class Batch implements Serializable {
@XmlElement(name = "Documents")
private Documents documents;
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public static class Documents implements Serializable {
@XmlElement(name = "Document")
private Document document;
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public static class Document implements Serializable {
@XmlElement(name = "IndexFields")
private IndexFields indexFields;
@XmlElement(name = "Pages")
private Pages pages;
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public static class IndexFields implements Serializable {
@XmlElement(name = "IndexField")
private List<IndexField> indexField;
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public static class IndexField implements Serializable {
@XmlAttribute(name = "Name")
private String name;
@XmlAttribute(name = "Value")
private String value;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public static class Pages implements Serializable {
@XmlElement(name = "Page")
private List<Page> page;
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public static class Page implements Serializable {
@XmlAttribute(name = "ImportFileName")
private String importFileName;
}
}
}
}
}
}
}
下面是我的解组代码:
private static void main(String xmlFile) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(ImportSession.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
ImportSession ImportSession = (ImportSession) unmarshaller.unmarshal(new File(xmlFile));
System.out.println("output = " + ImportSession);
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
在我的文档中 class,如果我有
private Document document;
它工作正常,但它只给了我 xml 的最后一个文档。但是如果我尝试输入:
private List<Document> document;
代码刚刚出来,什么也没有,也没有错误。需要帮助分析我遗漏的内容。
我不想在我的输出中使用 BatchFields/BatchField,所以我的 class 架构中没有相同的内容。
我用 xjc
生成了 java class ImportSession.java
并得到了不同的结果。
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"batches"
})
@XmlRootElement(name = "ImportSession")
public class ImportSession {
@XmlElement(name = "Batches", required = true)
protected ImportSession.Batches batches;
public ImportSession.Batches getBatches() {
return batches;
}
public void setBatches(ImportSession.Batches value) {
this.batches = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"batch"
})
public static class Batches {
@XmlElement(name = "Batch", required = true)
protected ImportSession.Batches.Batch batch;
public ImportSession.Batches.Batch getBatch() {
return batch;
}
public void setBatch(ImportSession.Batches.Batch value) {
this.batch = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"batchFields",
"documents"
})
public static class Batch {
@XmlElement(name = "BatchFields", required = true)
protected ImportSession.Batches.Batch.BatchFields batchFields;
@XmlElement(name = "Documents", required = true)
protected ImportSession.Batches.Batch.Documents documents;
@XmlAttribute(name = "BatchClassName", required = true)
protected String batchClassName;
public ImportSession.Batches.Batch.BatchFields getBatchFields() {
return batchFields;
}
public void setBatchFields(ImportSession.Batches.Batch.BatchFields value) {
this.batchFields = value;
}
public ImportSession.Batches.Batch.Documents getDocuments() {
return documents;
}
public void setDocuments(ImportSession.Batches.Batch.Documents value) {
this.documents = value;
}
public String getBatchClassName() {
return batchClassName;
}
public void setBatchClassName(String value) {
this.batchClassName = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"batchField"
})
public static class BatchFields {
@XmlElement(name = "BatchField", required = true)
protected List<ImportSession.Batches.Batch.BatchFields.BatchField> batchField;
public List<ImportSession.Batches.Batch.BatchFields.BatchField> getBatchField() {
if (batchField == null) {
batchField = new ArrayList<ImportSession.Batches.Batch.BatchFields.BatchField>();
}
return this.batchField;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class BatchField {
@XmlAttribute(name = "Name", required = true)
protected String name;
@XmlAttribute(name = "Value", required = true)
protected String value;
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"document"
})
public static class Documents {
@XmlElement(name = "Document", required = true)
protected List<ImportSession.Batches.Batch.Documents.Document> document;
public List<ImportSession.Batches.Batch.Documents.Document> getDocument() {
if (document == null) {
document = new ArrayList<ImportSession.Batches.Batch.Documents.Document>();
}
return this.document;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"indexFields",
"pages"
})
public static class Document {
@XmlElement(name = "IndexFields", required = true)
protected ImportSession.Batches.Batch.Documents.Document.IndexFields indexFields;
@XmlElement(name = "Pages", required = true)
protected ImportSession.Batches.Batch.Documents.Document.Pages pages;
@XmlAttribute(name = "FormTypeName", required = true)
protected String formTypeName;
public ImportSession.Batches.Batch.Documents.Document.IndexFields getIndexFields() {
return indexFields;
}
public void setIndexFields(ImportSession.Batches.Batch.Documents.Document.IndexFields value) {
this.indexFields = value;
}
public ImportSession.Batches.Batch.Documents.Document.Pages getPages() {
return pages;
}
public void setPages(ImportSession.Batches.Batch.Documents.Document.Pages value) {
this.pages = value;
}
public String getFormTypeName() {
return formTypeName;
}
public void setFormTypeName(String value) {
this.formTypeName = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"indexField"
})
public static class IndexFields {
@XmlElement(name = "IndexField", required = true)
protected List<ImportSession.Batches.Batch.Documents.Document.IndexFields.IndexField> indexField;
public List<ImportSession.Batches.Batch.Documents.Document.IndexFields.IndexField> getIndexField() {
if (indexField == null) {
indexField = new ArrayList<ImportSession.Batches.Batch.Documents.Document.IndexFields.IndexField>();
}
return this.indexField;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class IndexField {
@XmlAttribute(name = "Name", required = true)
protected String name;
@XmlAttribute(name = "Value", required = true)
protected String value;
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"page"
})
public static class Pages {
@XmlElement(name = "Page", required = true)
protected ImportSession.Batches.Batch.Documents.Document.Pages.Page page;
public ImportSession.Batches.Batch.Documents.Document.Pages.Page getPage() {
return page;
}
public void setPage(ImportSession.Batches.Batch.Documents.Document.Pages.Page value) {
this.page = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class Page {
@XmlAttribute(name = "ImportFileName", required = true)
protected String importFileName;
public String getImportFileName() {
return importFileName;
}
public void setImportFileName(String value) {
this.importFileName = value;
}
}
}
}
}
}
}
}
我首先使用此在线站点从您的 xml
文件生成了 xsd
文件:https://www.liquid-technologies.com/online-xml-to-xsd-converter
生成了xsd
(我命名为“test.xsd”):
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Technologies Online Tools 1.0 (https://www.liquid-technologies.com) -->
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ImportSession">
<xs:complexType>
<xs:sequence>
<xs:element name="Batches">
<xs:complexType>
<xs:sequence>
<xs:element name="Batch">
<xs:complexType>
<xs:sequence>
<xs:element name="BatchFields">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="BatchField">
<xs:complexType>
<xs:attribute name="Name" type="xs:string" use="required" />
<xs:attribute name="Value" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Documents">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Document">
<xs:complexType>
<xs:sequence>
<xs:element name="IndexFields">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="IndexField">
<xs:complexType>
<xs:attribute name="Name" type="xs:string" use="required" />
<xs:attribute name="Value" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Pages">
<xs:complexType>
<xs:sequence>
<xs:element name="Page">
<xs:complexType>
<xs:attribute name="ImportFileName" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="FormTypeName" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="BatchClassName" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
生成ImportSession.java
(xjc
包含在JDK8中):
$ xjc test.xsd
analyse dun schéma...
compilation dun schéma...
generated/ImportSession.java
generated/ObjectFactory.java
我删除了第一行 package generated;
和许多自动生成的评论。
现在,
System.out.println("output = " + ImportSession.getBatches().getBatch().getDocuments().getDocument().size());
显示 3
.
您的 <Documents>
XML 元素包含几个 <Document>
元素。
因此在您的 Documents
Java class 中您不需要
Document
类型的成员,而不是 List<Document>
类型的成员。
所以在你的 Documents
class 你需要替换
@XmlElement(name = "Document")
private Document document;
来自
@XmlElement(name = "Document")
private List<Document> documents;
我也把会员名从document
改成了documents
,
因为使用复数词是列表的常见做法。
虽然上面的修改已经解决了你的问题,
还有更多的改进空间:
目前您有两个独立的 classes Documents
和
Document
用于建模 XML 内容如:
<Documents>
<Document ...>...</Document>
<Document ...>...</Document>
<Document ...>...</Document>
</Documents>
您可以使用 @XmlElementWrapper
简化 Java 代码
注解。在你的 Batch
class 你可以替换
@XmlElement(name = "Documents")
private Documents documents;
来自
@XmlElementWrapper(name = "Documents")
@XmlElement(name = "Document")
private List<Document> documents;
这样您就不再需要 Documents
class。
您可能想要进行类似的修改
对于 Java 代码中对应于
的其他列表
<Batches>
包含几个 <Batch>
元素
<BatchFields>
包含几个 <BatchField>
元素
<IndexFields>
包含几个 <IndexField>
元素
<Pages>
包含几个 <Page>
元素
我正在尝试解组这个复杂的 xml,但未能成功。下面是我的 xml:
<ImportSession xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Batches>
<Batch BatchClassName="BC">
<BatchFields>
<BatchField Name="N11" Value="N21"/>
<BatchField Name="N12" Value="N22"/>
<BatchField Name="N13" Value="N23"/>
<BatchField Name="N14" Value="N24"/>
<BatchField Name="N15" Value="N25"/>
</BatchFields>
<Documents>
<Document FormTypeName="F1">
<IndexFields>
<IndexField Name="NM11" Value="V11"/>
<IndexField Name="NM12" Value="V12"/>
<IndexField Name="NM13" Value="V13"/>
<IndexField Name="NM14" Value="V14"/>
</IndexFields>
<Pages>
<Page ImportFileName="P1.pdf"/>
</Pages>
</Document>
<Document FormTypeName="F2">
<IndexFields>
<IndexField Name="NM21" Value="V21"/>
<IndexField Name="NM22" Value="V22"/>
<IndexField Name="NM23" Value="V23"/>
<IndexField Name="NM24" Value="V24"/>
</IndexFields>
<Pages>
<Page ImportFileName="P2.pdf"/>
</Pages>
</Document>
<Document FormTypeName="F3">
<IndexFields>
<IndexField Name="NM31" Value="V31"/>
<IndexField Name="NM32" Value="V32"/>
<IndexField Name="NM33" Value="V33"/>
<IndexField Name="NM34" Value="V34"/>
</IndexFields>
<Pages>
<Page ImportFileName="P3.pdf"/>
</Pages>
</Document>
</Documents>
</Batch>
</Batches>
</ImportSession>
这是我的ImportSession.java
import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.*;
import lombok.Getter;
import lombok.Setter;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.ToString;
@XmlRootElement(name = "ImportSession")
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public class ImportSession {
@XmlElement(name = "Batches")
private Batches batches;
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public static class Batches implements Serializable {
@XmlElement(name = "Batch")
private Batch batch;
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public static class Batch implements Serializable {
@XmlElement(name = "Documents")
private Documents documents;
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public static class Documents implements Serializable {
@XmlElement(name = "Document")
private Document document;
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public static class Document implements Serializable {
@XmlElement(name = "IndexFields")
private IndexFields indexFields;
@XmlElement(name = "Pages")
private Pages pages;
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public static class IndexFields implements Serializable {
@XmlElement(name = "IndexField")
private List<IndexField> indexField;
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public static class IndexField implements Serializable {
@XmlAttribute(name = "Name")
private String name;
@XmlAttribute(name = "Value")
private String value;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public static class Pages implements Serializable {
@XmlElement(name = "Page")
private List<Page> page;
@XmlAccessorType(XmlAccessType.FIELD)
@ToString
public static class Page implements Serializable {
@XmlAttribute(name = "ImportFileName")
private String importFileName;
}
}
}
}
}
}
}
下面是我的解组代码:
private static void main(String xmlFile) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(ImportSession.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
ImportSession ImportSession = (ImportSession) unmarshaller.unmarshal(new File(xmlFile));
System.out.println("output = " + ImportSession);
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
在我的文档中 class,如果我有
private Document document;
它工作正常,但它只给了我 xml 的最后一个文档。但是如果我尝试输入:
private List<Document> document;
代码刚刚出来,什么也没有,也没有错误。需要帮助分析我遗漏的内容。
我不想在我的输出中使用 BatchFields/BatchField,所以我的 class 架构中没有相同的内容。
我用 xjc
生成了 java class ImportSession.java
并得到了不同的结果。
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"batches"
})
@XmlRootElement(name = "ImportSession")
public class ImportSession {
@XmlElement(name = "Batches", required = true)
protected ImportSession.Batches batches;
public ImportSession.Batches getBatches() {
return batches;
}
public void setBatches(ImportSession.Batches value) {
this.batches = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"batch"
})
public static class Batches {
@XmlElement(name = "Batch", required = true)
protected ImportSession.Batches.Batch batch;
public ImportSession.Batches.Batch getBatch() {
return batch;
}
public void setBatch(ImportSession.Batches.Batch value) {
this.batch = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"batchFields",
"documents"
})
public static class Batch {
@XmlElement(name = "BatchFields", required = true)
protected ImportSession.Batches.Batch.BatchFields batchFields;
@XmlElement(name = "Documents", required = true)
protected ImportSession.Batches.Batch.Documents documents;
@XmlAttribute(name = "BatchClassName", required = true)
protected String batchClassName;
public ImportSession.Batches.Batch.BatchFields getBatchFields() {
return batchFields;
}
public void setBatchFields(ImportSession.Batches.Batch.BatchFields value) {
this.batchFields = value;
}
public ImportSession.Batches.Batch.Documents getDocuments() {
return documents;
}
public void setDocuments(ImportSession.Batches.Batch.Documents value) {
this.documents = value;
}
public String getBatchClassName() {
return batchClassName;
}
public void setBatchClassName(String value) {
this.batchClassName = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"batchField"
})
public static class BatchFields {
@XmlElement(name = "BatchField", required = true)
protected List<ImportSession.Batches.Batch.BatchFields.BatchField> batchField;
public List<ImportSession.Batches.Batch.BatchFields.BatchField> getBatchField() {
if (batchField == null) {
batchField = new ArrayList<ImportSession.Batches.Batch.BatchFields.BatchField>();
}
return this.batchField;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class BatchField {
@XmlAttribute(name = "Name", required = true)
protected String name;
@XmlAttribute(name = "Value", required = true)
protected String value;
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"document"
})
public static class Documents {
@XmlElement(name = "Document", required = true)
protected List<ImportSession.Batches.Batch.Documents.Document> document;
public List<ImportSession.Batches.Batch.Documents.Document> getDocument() {
if (document == null) {
document = new ArrayList<ImportSession.Batches.Batch.Documents.Document>();
}
return this.document;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"indexFields",
"pages"
})
public static class Document {
@XmlElement(name = "IndexFields", required = true)
protected ImportSession.Batches.Batch.Documents.Document.IndexFields indexFields;
@XmlElement(name = "Pages", required = true)
protected ImportSession.Batches.Batch.Documents.Document.Pages pages;
@XmlAttribute(name = "FormTypeName", required = true)
protected String formTypeName;
public ImportSession.Batches.Batch.Documents.Document.IndexFields getIndexFields() {
return indexFields;
}
public void setIndexFields(ImportSession.Batches.Batch.Documents.Document.IndexFields value) {
this.indexFields = value;
}
public ImportSession.Batches.Batch.Documents.Document.Pages getPages() {
return pages;
}
public void setPages(ImportSession.Batches.Batch.Documents.Document.Pages value) {
this.pages = value;
}
public String getFormTypeName() {
return formTypeName;
}
public void setFormTypeName(String value) {
this.formTypeName = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"indexField"
})
public static class IndexFields {
@XmlElement(name = "IndexField", required = true)
protected List<ImportSession.Batches.Batch.Documents.Document.IndexFields.IndexField> indexField;
public List<ImportSession.Batches.Batch.Documents.Document.IndexFields.IndexField> getIndexField() {
if (indexField == null) {
indexField = new ArrayList<ImportSession.Batches.Batch.Documents.Document.IndexFields.IndexField>();
}
return this.indexField;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class IndexField {
@XmlAttribute(name = "Name", required = true)
protected String name;
@XmlAttribute(name = "Value", required = true)
protected String value;
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"page"
})
public static class Pages {
@XmlElement(name = "Page", required = true)
protected ImportSession.Batches.Batch.Documents.Document.Pages.Page page;
public ImportSession.Batches.Batch.Documents.Document.Pages.Page getPage() {
return page;
}
public void setPage(ImportSession.Batches.Batch.Documents.Document.Pages.Page value) {
this.page = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class Page {
@XmlAttribute(name = "ImportFileName", required = true)
protected String importFileName;
public String getImportFileName() {
return importFileName;
}
public void setImportFileName(String value) {
this.importFileName = value;
}
}
}
}
}
}
}
}
我首先使用此在线站点从您的 xml
文件生成了 xsd
文件:https://www.liquid-technologies.com/online-xml-to-xsd-converter
生成了xsd
(我命名为“test.xsd”):
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Technologies Online Tools 1.0 (https://www.liquid-technologies.com) -->
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ImportSession">
<xs:complexType>
<xs:sequence>
<xs:element name="Batches">
<xs:complexType>
<xs:sequence>
<xs:element name="Batch">
<xs:complexType>
<xs:sequence>
<xs:element name="BatchFields">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="BatchField">
<xs:complexType>
<xs:attribute name="Name" type="xs:string" use="required" />
<xs:attribute name="Value" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Documents">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Document">
<xs:complexType>
<xs:sequence>
<xs:element name="IndexFields">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="IndexField">
<xs:complexType>
<xs:attribute name="Name" type="xs:string" use="required" />
<xs:attribute name="Value" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Pages">
<xs:complexType>
<xs:sequence>
<xs:element name="Page">
<xs:complexType>
<xs:attribute name="ImportFileName" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="FormTypeName" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="BatchClassName" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
生成ImportSession.java
(xjc
包含在JDK8中):
$ xjc test.xsd
analyse dun schéma...
compilation dun schéma...
generated/ImportSession.java
generated/ObjectFactory.java
我删除了第一行 package generated;
和许多自动生成的评论。
现在,
System.out.println("output = " + ImportSession.getBatches().getBatch().getDocuments().getDocument().size());
显示 3
.
您的 <Documents>
XML 元素包含几个 <Document>
元素。
因此在您的 Documents
Java class 中您不需要
Document
类型的成员,而不是 List<Document>
类型的成员。
所以在你的 Documents
class 你需要替换
@XmlElement(name = "Document")
private Document document;
来自
@XmlElement(name = "Document")
private List<Document> documents;
我也把会员名从document
改成了documents
,
因为使用复数词是列表的常见做法。
虽然上面的修改已经解决了你的问题, 还有更多的改进空间:
目前您有两个独立的 classes Documents
和
Document
用于建模 XML 内容如:
<Documents>
<Document ...>...</Document>
<Document ...>...</Document>
<Document ...>...</Document>
</Documents>
您可以使用 @XmlElementWrapper
简化 Java 代码
注解。在你的 Batch
class 你可以替换
@XmlElement(name = "Documents")
private Documents documents;
来自
@XmlElementWrapper(name = "Documents")
@XmlElement(name = "Document")
private List<Document> documents;
这样您就不再需要 Documents
class。
您可能想要进行类似的修改 对于 Java 代码中对应于
的其他列表<Batches>
包含几个<Batch>
元素<BatchFields>
包含几个<BatchField>
元素<IndexFields>
包含几个<IndexField>
元素<Pages>
包含几个<Page>
元素