Reputation: 1108
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 : )!
Upvotes: 3
Views: 5345
Reputation: 148977
You can use @XmlID
to map a property as the key and @XmlIDREF
to map the reference to the key for this use case.
Step
@XmlAccessorType(XmlAccessType.FIELD)
public class Step {
@XmlID
@XmlAttribute
private String _id;
}
Path
@XmlAccessorType(XmlAccessType.FIELD)
public class Path {
@XmlIDREF
@XmlAttribute
private Step _reference;
}
For More Information
UPDATE
Thanks! I Completely missed your article. I've extended my question, do you have any clue if this is possible too? I do not want to create a class with only holding the reference, I'd like to store it inside the step class.
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 @XmlPath
annotation for your use case.
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlAccessorType(XmlAccessType.FIELD)
public class Step {
@XmlID
@XmlAttribute
private String id;
@XmlPath("DetailPanelReference/@reference")
@XmlIDREF
// private List<DetailPanel> _detailPanels; // WORKS
private DetailPanel[] _detailPanels; // See bug: http://bugs.eclipse.org/399293
}
For More Information
Upvotes: 7