Here is what I have so far to marshall my POJO using JAXB :
@XmlRootElement
public class Local {
private Entity entity;
public void setEntity(Entity entity) {
this.entity = entity;
}
@XmlElement
public Entity getEntity() {
return entity;
}
}
and
@XmlRootElement
public class Entity {
private String name;
private String comment;
public void setName(String name){
this.name = name;
}
@XmlAttribute
public String getName(){
return this.name;
}
public void setComment...
@XmlAttribute
public void getComment...
}
With that, I get something like this:
<local>
<entity name="" comment=""></entity>
</local>
However, I would prefer to have the name attribute as an attribute of the local:
<local entityName="" entityComment=""></local>
Is the XmlJavaTypeAdapter a good way to begin with?
Thanks,
Alex
There are a couple of different options to handle this use case:
Option #1 – XmlAdapter (Any JAXB implementation)
You could use an
XmlAdapterfor this use case. This will work as long as only one attribute value comes from theEntityobject:EntityAdapter
Local
The XmlAdapter is linked with the field/property using the
@XmlJavaTypeAdapterannotation:For More Information
Option #2 – @XmlPath (EclipseLink JAXB (MOXy)
Alternatively if you are using EclipseLink JAXB (MOXy), the you could use the
@XmlPathextension. This is useful with theEntityobject corresponds to multiple XML attributes:Local
Specifying the XPath “.” indicated that the child contents will be written into the parent element
Entity
For More Information