java - How can I make a class field become a tag name using JAXB? -
java - How can I make a class field become a tag name using JAXB? -
i using java , jaxb xml processing.
i have next class:
public class characteristic { private string characteristic; private string value; @xmlattribute public string getcharacteristic() { homecoming characteristic; } public void setcharacteristic(string characteristic) { this.characteristic = characteristic; } @xmlvalue public string getvalue() { homecoming value; } public void setvalue(string value) { this.value = value; } } public static void main(string[] args) { characteristic c = new characteristic(); c.setcharacteristic("store_capacity"); c.setvalue(40); characteristic c2 = new characteristic(); c2.setcharacteristic("number_of_doors"); c2.setvalue(4); }
this result get:
<characteristics characteristic="store_capacity">40</characteristics> <characteristics characteristic="number_of_doors">4</characteristics>
i want next result:
<store_capacity>40</store_capacity> <number_of_doors>4</number_of_doors>
how can accomplish this?
you can utilize combination of @xmlelementref , jaxbelement produce dynamic element names.
the thought is:
makecharacteristic
subclass of jaxbelement
, override getname()
method homecoming name based on characteristic
property. annotate characteristics
@xmlelementref
. provide @xmlregistry
(objectfactory
) @xmlelementdecl(name = "characteristic")
. below working test.
the test (nothing special):
@test public void marshallsdynamicelementname() throws jaxbexception { jaxbcontext context = jaxbcontext.newinstance(objectfactory.class); final characteristics characteristics = new characteristics(); final characteristic characteristic = new characteristic( "store_capacity", "40"); characteristics.getcharacteristics().add(characteristic); context.createmarshaller().marshal(characteristics, system.out); }
produces:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <characteristics><store_capacity>40</store_capacity></characteristics>
let's start characteristics
root element class. has characteristics
property annotated @xmlelementref
. means contents should either jaxbelement
s or @xmlrootelement
-annotated class instances.
@xmlrootelement(name = "characteristics") public static class characteristics { private final list<characteristic> characteristics = new linkedlist<characteristic>(); @xmlelementref(name = "characteristic") public list<characteristic> getcharacteristics() { homecoming characteristics; } }
in order work need objectfactory
or annotated @xmlregistry
having corresponding @xmlelementdecl
:
@xmlregistry public static class objectfactory { @xmlelementdecl(name = "characteristic") public jaxbelement<string> createcharacteristic(string value) { homecoming new characteristic(value); } }
recall, characteristics
property must contain either @xmlrootelement
-annotated class instances or jaxbelement
s. @xmlrootelement
not suitable since it's static. jaxbelement
dynamic. can subclass jaxbelement
, override getname()
method:
public static class characteristic extends jaxbelement<string> { private static final long serialversionuid = 1l; public static final qname name = new qname("characteristic"); public characteristic(string value) { super(name, string.class, value); } public characteristic(string characteristic, string value) { super(name, string.class, value); this.characteristic = characteristic; } @override public qname getname() { final string characteristic = getcharacteristic(); if (characteristic != null) { homecoming new qname(characteristic); } homecoming super.getname(); } private string characteristic; @xmltransient public string getcharacteristic() { homecoming characteristic; } public void setcharacteristic(string characteristic) { this.characteristic = characteristic; } } }
in case i've overridden getname()
method dynamically determine element name. if characteristic
property set, value used name, otherwise method opts default characteristic
element.
the code of test available on github.
java xml jaxb
Comments
Post a Comment