Consider the following xml:
<Config>
<Paths>
<Path reference="WS_License"/>
</Paths>
<Steps>
<Step id="WS_License" title="License Agreement" />
</Steps>
</Config>
The following JAXB classes:
public class Path {
private String _reference;
public String getReference() {
return _reference;
}
@XmlAttribute
public void setReference( String reference ) {
_reference = reference;
}
}
And
public class Step {
private String _id;
private String _title;
public String getId() {
return _id;
}
@XmlAttribute
public void setId( String id ) {
_id = id;
}
public String getTitle() {
return _title;
}
@XmlAttribute
public void setTitle( String title ) {
_title = title;
}
}
Instead of storing the reference in the Path object as String, I’d like to hold it as a Step object. The link between those objects is the reference and id attributes. Is the @XMLJavaTypeAdapter attribute the way to go? Could anyone be so kind to provide an example of the correct usage?
Thanks!
EDIT:
I’d also would like to do the same technique with an element.
Consider the following xml:
<Config>
<Step id="WS_License" title="License Agreement">
<DetailPanelReference reference="DP_License" />
</Step>
<DetailPanels>
<DetalPanel id="DP_License" title="License Agreement" />
</DetailPanels>
</Config>
The following JAXB classes:
@XmlAccessorType(XmlAccessType.FIELD)
public class Step {
@XmlID
@XmlAttribute(name="id")
private String _id;
@XmlAttribute(name="title")
private String _title;
@XmlIDREF
@XmlElement(name="DetailPanelReference", type=DetailPanel.class)
private DetailPanel[] _detailPanels; //Doesn't seem to work
}
@XmlAccessorType(XmlAccessType.FIELD)
public class DetailPanel {
@XmlID
@XmlAttribute(name="id")
private String _id;
@XmlAttribute(name="title")
private String _title;
}
The property _detailPanels in the Step-object is empty and the link doesn’t seems to work. Is there any option to create a link without creating a new JAXB object holding only the reference to the DetailPanel?
Thanks again : )!
You can use
@XmlIDto map a property as the key and@XmlIDREFto map the reference to the key for this use case.Step
Path
For More Information
UPDATE
Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
If you are using MOXy as your JAXB (JSR-222) provider then you could leverage the
@XmlPathannotation for your use case.For More Information