Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UnknownXmlFieldException] How to ignore unspecified fields when deserializing? #257

Closed
Rashxz opened this issue Nov 29, 2024 · 3 comments
Closed
Labels
question Further information is requested

Comments

@Rashxz
Copy link

Rashxz commented Nov 29, 2024

Hi, Im tryng to deserialze a xml response that have a lot of fields, but I will only be using a few, so dont what to specify all of them as that will be time consuming and having only necesary fields will make it easier to read.

This is my class:

@Serializable
@XmlSerialName("Z_WEBCOM_CONSULTAResponse", "urn:sap-com:document:sap:rfc:functions", "n0")
data class SearchOrdersDTO(
    @XmlSerialName("CLIENTES", "", "")
    @XmlElement(true)
    val clients: ClientsSection,

    @XmlSerialName("TVBAK", "", "")
    @XmlElement(true)
    val tvBak: TvBakSection,
){
    @Serializable
    data class ClientsSection(
        @XmlSerialName("item", "", "")
        @XmlElement(true)
        val items: List<ClientDataItem>
    ){
        @Serializable
        data class ClientDataItem(
            @XmlSerialName("KUNNR", "", "")
            @XmlElement(true)
            val clientCode: String,

            @XmlSerialName("NAME1", "", "")
            @XmlElement(true)
            val name: String,
        )
    }
}

If i put all of the fields of ClientDataItem the deserialization works just fine, but if I only leave a fews I got the UnknownXmlFieldException exception. So is there anyway to just ignore the rest of fields when deserializing?

@pdvrieze
Copy link
Owner

In the policy you can add a handler for unknown fields/elements. This returns a list of the recovered values (which can be an empty list) or throws an exception (the default). However, see #256, there is an issue with it in the case of a value child (the @XmlValue annotation).

@pdvrieze pdvrieze added the question Further information is requested label Nov 29, 2024
@pdvrieze
Copy link
Owner

pdvrieze commented Nov 29, 2024

An alternate approach is to use the fallback options (@XmlOtherAttributes with either Map<QName,String> or Map<String,String> - the string is default, but it should work for anything that can parse from a string), and for elements an @XmlValue List<Node> or List<Element> (or other type with a serializer implementing XmlDeserializationStrategy (this needed to consume the random xml)).

This approach will actually record the values in a generic way (which can be written out).

@Rashxz
Copy link
Author

Rashxz commented Nov 29, 2024

Thanks, the policy works perfectly.

@Rashxz Rashxz closed this as completed Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants