pdvrieze / xmlutil

XML Serialization library for Kotlin
https://pdvrieze.github.io/xmlutil/
Apache License 2.0
363 stars 30 forks source link

Add Multiple NameSpaces for a XML Element #178

Closed LogeswaranRamasamy closed 6 months ago

LogeswaranRamasamy commented 10 months ago

I am trying to configure multiple namespaces to a XML Element inorder but couldn't find a option to achieve it using Xml Serialization.

Current code:

@Serializable
    @XmlSerialName("breakfast_menu", "breakfast", "b")
    data class FoodMenu (
        @XmlSerialName("food", "", "" )
        var foodList : MutableList<Food> ?= null
    )

Trying to achieve the Below which doesn't seem possible:

@Serializable
    @XmlSerialName("breakfast_menu", "breakfast", "b")
    @XmlSerialName("lunch_menu", "lunch", "l")
    data class FoodMenu (
        @XmlSerialName("food", "", "" )
        var foodList : MutableList<Food> ?= null
    )

Is there any option to achieve it ? If so, how to achieve the same ? Else is there any alternate workaround to achieve the same ?

pdvrieze commented 10 months ago

There are two separate ways of naming supported. One is for the type. Types only have a single name (although you could filter your reader to switch names). However, you can also specify the name in the use context (on the properties), in which case the default policy uses that as the priority.

For top-level you can pass along the root tag name to use, and do it that way. For reading the top level tag is automatically detected from the xml if you don't specify it.

A final thing you may want to do is to introduce some namespaces. If you use the @XmlNamespaces annotation you can specify the namespaces to be generated for that tag (which means your xml is not polluted with all kinds of namespace attributes all over the place).

pdvrieze commented 6 months ago

This should now be supported using a namespaces annotation.

Morteza-Rastgoo commented 6 months ago

@pdvrieze Can't find the XmlNamespaces or namespaces annotations. Could you please elaborate?

pdvrieze commented 6 months ago

Sorry, I didn't remember the name properly. See https://pdvrieze.github.io/xmlutil/serialization/nl.adaptivity.xmlutil.serialization/-xml-namespace-decl-spec/index.html