I have a class, that is something like this:
public class Property {
private double floorArea;
public double getFloorArea() {
return floorArea;
}
@XmlElement
public void setFloorArea(double floorArea) {
this.floorArea = floorArea;
}
}
Which will give me something like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<property>
<floorArea>x</floorArea>
</property>
But I need something like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<property>
<floorArea>
<value>x</value>
</floorArea>
</property>
The API I am using requires it this way. My limited JAXB knowledge is preventing me from figuring this out. Any help is appreciated.
EDIT:
something I am researching. Would I need to create a value class with its own JAXB annotations for this to work? (and set floorArea to the type of value)?
Below is how your use case could be supported with an
XmlAdapterusing any JAXB (JSR-222) implementation.XmlAdapter (DoubleValueAdapter)
An
XmlAdapteris a mechanism that allows an object to be converted to another type of object . Then it is the converted object that is converted to/from XML.Property
The
@XmlJavaTypeAdapterannotation is used to specify theXmlAdapter. I needed to changedoubletoDoubleso I moved the mapping to the field as to not affect the public API.Demo
Below is some demo code to prove that everything works.
input.xml/Output
Below is the input to and output from the demo code.