I am trying to unmarshall this complex xml, but not able to do it successfully. Below is my 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>
This is my 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;
}
}
}
}
}
}
}
Below is my unmarshalling code:
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();
}
}
In my Documents class, if I have
private Document document;
It works fine, but then it only gives me last document of my xml. But if I try to put:
private List<Document> document;
Code just comes out with nothing, no errors as well. Need help in analyzing what I am missing.
I don't want BatchFields/BatchField in my output, so I don't have that same in my class schema.