1

How shoud I correctly declare object to parse it from XML? Problem with field ProblemField which is found several times. I try to parse it using xmlMapper.readValue(content, ParentObject.class) but List<DataField> problemField is null.

Input XML sample:

<ParentObject>
    <ChildObject>
        <PersonalId title="Personal ID" value="123456789"/>
        <Name title="sample value"
              value="sample value"/>
    </ChildObject>
    <ChildObject>
        <Sum title="Chapter title 1" value="452.00"/>
        <ProblemField id="100"
             title="100 | sample title"
             value="4524.00"/>
        <ProblemField id="101"
             title="101 | sample title"
             value="145224.00"/>
        <ProblemField id="102" title="102 | sample title"
             value="-71857.00"/>
    </ChildObject>
    <ChildObject>
        <Sum title="Chapter title 2" value="78578.00"/>
        <ProblemField id="100"
             title="100 | sample title"
             value="4152452.00"/>
        <ProblemField id="101"
             title="101 | sample title"
             value="785178.00"/>
    </ChildObject>
</ParentObject>

Objects declaration

ParentObject:

@JsonIgnoreProperties(ignoreUnknown = true)
public class ParentObject {
    @JsonProperty(value = "PersonalId")
    private DataField personalId;
    @JsonProperty(value = "Name")
    private DataField name;
    @JsonProperty(value = "Sum")
    private DataField sum;
    @JsonProperty(value = "ProblemField")
    private List<DataField> problemField;
}

DataField:

@JsonIgnoreProperties(ignoreUnknown = true)
public class PkbDataField {
    @JsonProperty(value = "title")
    private String title;
    @JsonProperty(value = "value")
    private String value;
}
Shisui
  • 111
  • 4
  • 1
    Your class structure doesn't match the XML. – M. Deinum May 24 '22 at 13:02
  • I know, how can I fix it? – Shisui May 25 '22 at 04:03
  • By fixing your java structure. `ParentObject` should have a list of `ChildObject` instances . However generally when using XML there is also an XSD which describes the structure. You can use that XSD to generate the java classes for mapping. – M. Deinum May 25 '22 at 05:35

0 Answers0