Search code examples
javaxmljackson-dataformat-xml

xml file in mixed order


The sequence of the elements in the xml file are generated mixed. For this reason, when pulling data with jackson, there is only one element in the list. How can I pull the whole list even if the element order is mixed in the XML file?

Xml file example:

<allcities id="p1">

    <cities id="x1" name="a">
        <info>x</info>
    </cities>

    <other id="1" gg="x" hh="y" />

    <cities id="y1" name="b">
        <info>x</info>
    </cities>

    <other id="2" gg="x" hh="y" />
    <other id="3" gg="x" hh="y" />

</allcities>

java code:

JacksonXmlModule xmlModule = new JacksonXmlModule();
xmlModule.setDefaultUseWrapper(false);
ObjectMapper objectMapper = new XmlMapper(xmlModule);
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
pojo = objectMapper.readValue(getXmlString(), allcities.class);
@NoArgsConstructor
@AllArgsConstructor
@Data
public final class list
{
    public List<cities> cities;
    public List<other> other;
}

Solution

  • I was having the same problem and came across this issue. Short answer is to use the @JsonMerge annotation on those properties like:

    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public final class list
    {
        @JsonMerge 
        public List<cities> cities;
        @JsonMerge
        public List<other> other;
    }