Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Jaxb equivalent of a Text node value?

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>.

like image 630
ra9r Avatar asked Sep 30 '09 03:09

ra9r


People also ask

What is @XmlAccessorType XmlAccessType field?

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.

What is XmlAccessorType?

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.

What is a JAXB element?

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.

What is @XmlAttribute in Java?

@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.


1 Answers

I'm not 100% sure about this, but try to use an @XmlValue annotation instead of @XmlElement.

like image 158
jarnbjo Avatar answered Oct 06 '22 09:10

jarnbjo