Question

I'm currently producing the following XML output:

<pizzas>
    <pizza>
        <id>ffab8c72-bace-4748-b010-a06d1b13ad84</id>
        <name>Erna</name>
        <description>Robust med kant</description>
        <price>69.0</price>
        <toppings></toppings>
    </pizza>
</pizzas>

I would like to have the id data in the element of the XML output. Like:

<pizzas>
    <pizza id="ffab8c72-bace-4748-b010-a06d1b13ad84">
        <name>Erna</name>
        <description>Robust med kant</description>
        <price>69.0</price>
        <toppings></toppings>
    </pizza>
</pizzas>

The current output is generated by marshalling over an ArrayList which contains pizza elements. The class that holds this list is annotated like this:

@XmlRootElement(name = "pizzas")
@XmlAccessorType(XmlAccessType.FIELD) 

and the ArrayList itself is annotated with: @XmlElement(name = "pizza")

-- So the ArrayList annotation is the one I need to change. But how in the world do I do that :-) - I have had a look at @XMLElementRef, thought of ways to update @XMLElement dynamically, but as far as I can see its name String needs to be static/constant. @XMLWrapper has also caught my eye....but but but.....

Furthermore, for info. Each pizza element is annotated as:

@XmlRootElement(name = "pizza")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "id", "name", "description", "price", "toppings" })

I hope someone here on Stackoverflow is able to assist me with a good piece of advice :-D

Looking forward to hear from you.

Kind regards /Lars Bingchong

Was it helpful?

Solution

You can just annotate your id property with @XmlAttribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top