pdvrieze/xmlutil

Add Multiple NameSpaces for a XML Element

Closed this issue · 4 comments

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 ?

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).

This should now be supported using a namespaces annotation.

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