I am looking to convert a class that looks like this ...
public class Amenity {
public String id;
public String value;
}
into the following XML using JaxB annotations:
<amenity id="id-string-here">value-string-here</amenity>
Does anyone know what annotation to use on the value
member variable to accomplish this? The closest I've gotten so far is:
@XmlRootElement
public class Amenity {
@XmlAttribute
public String id;
@XmlElement
public String value;
}
Unfortunately this approach doesn't allow me to specify that the value
member variable should not be rendered as its own tag <value></value>
.
The @XmlAccessorType(XmlAccessType. FIELD) annotation tells JAXB to bind all non-static, non-transient fields to XML unless annotated with @XmlTransient , which prevents the mapping of a field or property. With the XmlAccessType. FIELD setting, getter/setter pairs are not bound to XML unless explicitly annotated.
Annotation Type XmlAccessorTypeControls whether fields or Javabean properties are serialized by default. Usage. @XmlAccessorType annotation can be used with the following program elements: package. a top level class.
Introduction. The javax.xml.bind.JAXBElement class is the JAXB representation of an Xml Element.This class represents information about an Xml Element from both the element declaration within a schema and the element instance value within an xml document with the following properties − element's xml tag name.
@Retention(value=RUNTIME) @Target(value={FIELD,METHOD}) public @interface XmlAttribute. Maps a JavaBean property to a XML attribute. Usage. The @XmlAttribute annotation can be used with the following program elements: JavaBean property.
I'm not 100% sure about this, but try to use an @XmlValue
annotation instead of @XmlElement
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With