简体   繁体   中英

Dynamically add value to XMLRootElement or XMLElement annotation using JAXB

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

您可以使用@XmlAttribute注释您的id属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM