Reputation: 111
I'm receiving xml file from external API which I can't change. I'm using Spring job with Item Reader that retrieves Drugs one by one. My problem lies in unmarshalling into the list of Packs, how to annotate it properly so it is filled with objects contained between tag for ever drug so far best scenario was null as a List with configuration shown below. How to configure classes so drug contains list of Packs (or any other structure if this one isn't feasible)
<drugs xmlns="http://rejestrymedyczne.csioz.gov.pl/rpl/eksport-danych-v1.0">
<drug name="Zoledronic acid Fresenius Kabi" type="human" [more attributes...]>
<packs>
<pack size="1" [more attributes...]/>
<pack size="4" [more attributes...]/>
</packs>
</drug>
[more drugs...]
</drugs>
Class that represents drug.
//Lombok annotation generating getters/setters, constructors (boiler plate), shouldn't be harmful
@Data
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement(name = "drugs", namespace = "http://rejestrymedyczne.csioz.gov.pl/rpl/eksport-danych-v1.0")
@XmlAccessorType(XmlAccessType.FIELD)
public class ExternalDrug {
@XmlAttribute(name = "name")
private String name;
@XmlAttribute(name = "type")
private DrugType type;
@XmlElementWrapper(name = "packs")
@XmlElement(name = "pack")
private List<Pack> packs;
}
And here goes Pack
//Again boiler plate
@Data
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement(name = "Packs")
@XmlAccessorType(XmlAccessType.FIELD)
public class Pack {
@XmlAttribute(name = "size")
private List<Pack> ;
}
Please bear in mind that I translated files to English so they will be easier to read for other people however I might make some mistakes doing so.
Upvotes: 2
Views: 1270
Reputation: 4507
It shall work, after you add namespace to your XMLElementWrapper and List element like this
@XmlElementWrapper(name="packs", namespace = "http://rejestrymedyczne.csioz.gov.pl/rpl/eksport-danych-v1.0")
@XmlElement(name="pack", namespace ="http://rejestrymedyczne.csioz.gov.pl/rpl/eksport-danych-v1.0")
private List<Pack> packs;
Upvotes: 1