Skip to content

XML parser error with nested same element names #294

@softkot

Description

@softkot

Jackson version 2.9.5

I have a simple xml like

<levels>
    <sublevel>
        <id>1</id>
        <sublevel>Name A</sublevel>
    </sublevel>
    <sublevel>
        <id>2</id>
        <sublevel>Name B</sublevel>
    </sublevel>
</levels>

Note that child sublevel has a property sublevel (element with the same name as enclosing tag). That structure has an issue for XmlMapper causing an exception

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
 at [Source: (StringReader); line: 4, column: 9] (through reference chain: RootLevel["sublevel"]->java.util.ArrayList[0]->Sublevel["sublevel"])

XmlMapper configured as

                XmlMapper().apply {
                    registerModule(JaxbAnnotationModule())
                    registerModule(JacksonXmlModule())
                    enable(SerializationFeature.INDENT_OUTPUT)
                    disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
                }
                        .setDefaultUseWrapper(false)
                        .setSerializationInclusion(JsonInclude.Include.NON_NULL)
                        .setSerializationInclusion(JsonInclude.Include.NON_EMPTY)

And POJO objects annotated as

    @XmlAccessorType(XmlAccessType.NONE)
    data class RootLevel(
            @field:XmlElement val sublevel: List<Sublevel> = arrayListOf()
    ) : Serializable

    @XmlAccessorType(XmlAccessType.NONE)
    data class Sublevel(
            @field:XmlElement val id: String?=null,
            @field:XmlElement val sublevel: String?=null
    ) : Serializable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions