我有以下XML结构,用于创建Java对象。
<TABLE NAME="AB" ID="10" CODE="ABC123" RANK="102" YEAR="2022" TIMESTAMP="2021-05-11-16.19.44.572000"> <TABLE NAME="CD"> <ROW CODE="BCD456" SERIAL="A" ORDER="1" DESCRIPTION_1="TEST1" DESCRIPTION_2="TEST2 19" DESCRIPTION_3="TEST 3" STATUS_CODE="496" STATUS_INDICATOR="J" PRODUCT_NAME="SHIP "> <TABLE NAME="CD1"> <ROW CD1_CODE=" 11N" CD1_DESCRIPTION="T"/> <ROW CD1_CODE=" 2KA" CD1_DESCRIPTION="T"/> </TABLE> <TABLE NAME="CD2"> <ROW CD2_CODE="11" CD2_DESCRIPTION="----"/> <ROW CD2_CODE="12" CD2_DESCRIPTION="----"/> <ROW CD2_CODE="35" CD2_DESCRIPTION="----"/> </TABLE> <TABLE NAME="CD3"/> </TABLE> <TABLE NAME="EF" CODE="EFG789"> <ROW FILE="1" FILE_NAME="TEST.pdf" TIMESTAMP="2021-06-22T08:32:08.055854"> <FILE_DATA>TESTDATA</FILE_DATA> </TABLE> </TABLE>
我已经创建了一个XMLMapper,如下所示,它从Java检索值。对我来说,主要的问题是当我在同一个父元素下的XML中有多个 <TABLE> 元素时。
<TABLE>
在第一个场景中, <TABLE NAME="AB"> 有两个子表 <TABLE NAME="CD"> 和 <TABLE NAME="EF"> 。
<TABLE NAME="AB">
<TABLE NAME="CD">
<TABLE NAME="EF">
在第二个场景中, <TABLE NAME="CD"> 有三个子表 <TABLE NAME="CD1"> 、 <TABLE NAME="CD2"> 和 <TABLE NAME="CD3"> 以及后续的数据 <ROW> 。
<TABLE NAME="CD1">
<TABLE NAME="CD2">
<TABLE NAME="CD3">
<ROW>
我试图使用下面提供的XMLMapper代码以及相关的Java类来存储来自XML的相关数据。我能够为 <TABLE NAME = "CD1"> 及其子元素 <ROW> 验证存储在映射器中的数据
<TABLE NAME = "CD1">
我已经尝试过在这个 post 中给出的答案,但是它并不完全适用于我的场景。
现在,我正在尝试从 <TABLE NAME = "CD2"> 和`中检索和存储元素。这就是我收到一个错误声明的地方
<TABLE NAME = "CD2">
Multiple fields representing property "TABLE": createcsv.elements.CDRow#cd1Table vs createcsv.elements.CDRow#cd2Table
XMLMapperApp.java
public class XmlMapperApp { public static void main(String[] args) { try { File xmlFile = new File("./resource/test.xml").getAbsoluteFile(); XmlMapper xmlMapper = new XmlMapper(); xmlMapper.disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES); EmployeeXML employeeXML = xmlMapper.readValue(xmlFile, EmployeeXML.class); } catch (Exception ex) { System.out.println(ex.getMessage()); }
EmployeeXML.java
@AllArgsConstructor @NoArgsConstructor @JacksonXmlRootElement(localName = "TABLE") public class EmployeeXML { @JacksonXmlProperty(isAttribute = true, localName = "NAME") private String name; @JacksonXmlProperty(isAttribute = true, localName = "ID") private String id; @JacksonXmlProperty(isAttribute = true, localName = "CODE") private String code; @JacksonXmlProperty(isAttribute = true, localName = "RANK") private String rank; @JacksonXmlProperty(isAttribute = true, localName = "YEAR") private String year; @JacksonXmlProperty(isAttribute = true, localName = "TIMESTAMP") private String timestamp; @JacksonXmlProperty(localName = "TABLE") private CDTable cdTable; @JacksonXmlProperty(localName = "TABLE") private EFTable efTable; }
RowElement.java
public interface RowElement { }
CDTable.java
@NoArgsConstructor @AllArgsConstructor @JacksonXmlRootElement(localName = "TABLE") public class CDTable { @JacksonXmlProperty(isAttribute = true, localName = "NAME") private String name; @JacksonXmlProperty(localName = "ROW") private CDRow cdRow; }
CDRow.java
@NoArgsConstructor @AllArgsConstructor @JacksonXmlRootElement(localName = "ROW") public class CDRow implements RowElement { @JacksonXmlProperty(isAttribute = true, localName ="CODE") private String code; @JacksonXmlProperty(isAttribute = true, localName ="SERIAL") private String serial; @JacksonXmlProperty(isAttribute = true, localName ="ORDER") private String order; @JacksonXmlProperty(isAttribute = true, localName ="DESCRIPTION_1") private String benennung1; @JacksonXmlProperty(isAttribute = true, localName ="DESCRIPTION_2") private String benennung2; @JacksonXmlProperty(isAttribute = true, localName ="DESCRIPTION_3") private String benennung3; @JacksonXmlProperty(isAttribute = true, localName ="STATUS_CODE") private String statusCode; @JacksonXmlProperty(isAttribute = true, localName ="STATUS_INDICATOR") private String statusIndicator; @JacksonXmlProperty(isAttribute = true, localName ="PRODUCT_NAME") private String productName; @JacksonXmlProperty(localName = "TABLE") private CD1Table cd1Table; @JacksonXmlProperty(localName = "TABLE") private CD2Table cd2Table; @JacksonXmlProperty(localName = "TABLE") private CD3Table cd3Table; }
CD1Table.java
@NoArgsConstructor @AllArgsConstructor @JacksonXmlRootElement(localName = "TABLE") public class CD1Table { @JacksonXmlProperty(isAttribute = true, localName = "NAME") private String name; @JacksonXmlProperty(localName = "ROW") private List<CDRow> cd1Row; }
CD1Row.java
@NoArgsConstructor @AllArgsConstructor @JacksonXmlRootElement(localName = "ROW") public class CD1Row implements RowElement { @JacksonXmlProperty(isAttribute = true, localName ="CD1_CODE") private String cd1Code; @JacksonXmlProperty(isAttribute = true, localName ="CD1_DESCRIPTION") private String cd1Description; }
CD2Table.java
@NoArgsConstructor @AllArgsConstructor @JacksonXmlRootElement(localName = "TABLE") public class CD2Table { @JacksonXmlProperty(isAttribute = true, localName = "NAME") private String name; @JacksonXmlProperty(localName = "ROW") private List<CD2Row> cd2Row; }
CD2Row.java
@NoArgsConstructor @AllArgsConstructor @JacksonXmlRootElement(localName = "ROW") public class CD2Row implements RowElement { @JacksonXmlProperty(isAttribute = true, localName ="CD2_CODE") private String cd2Code; @JacksonXmlProperty(isAttribute = true, localName ="CD2_DESCRIPTION") private String cd2Description; @JacksonXmlProperty(localName = "TABLE") private CD3Table cd3Table; }
CD3Table.java
@NoArgsConstructor @AllArgsConstructor @JacksonXmlRootElement(localName = "TABLE") public class CD3Table { @JacksonXmlProperty(isAttribute = true, localName = "NAME") private String name; }
EFTable.java
@NoArgsConstructor @AllArgsConstructor @JacksonXmlRootElement(localName = "TABLE") public class EFTable { @JacksonXmlProperty(isAttribute = true, localName = "NAME") private String name; @JacksonXmlProperty(isAttribute = true, localName = "CODE") private String code; @JacksonXmlProperty(localName = "ROW") private EFRow efRow; }
EFRow.java
@NoArgsConstructor @AllArgsConstructor @JacksonXmlRootElement(localName = "ROW") public class EFRow implements RowElement { @JacksonXmlProperty(isAttribute = true, localName ="FILE") private String file; @JacksonXmlProperty(isAttribute = true, localName ="FILE_NAME")