Java – use JAXB to create reference objects based on attributes
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 as a string in the path object, I save it as a step object The links between these objects are the reference and ID attributes@ Is the xmljavatypeadapter property feasible? Can anyone be so kind as to provide examples of correct usage?
thank you!
Edit:
I also want to use elements to do the same technology
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;
}
Properties in step object_ Detailpanels is empty, links do not seem to work Is there any option to create a link without creating a new JAXB object that contains only a reference to the detailpanel?
Thanks again:)!
Solution
You can use @ xmlid to map attributes to keys and @ xmlidref to map references to keys for this use case
step
@XmlAccessorType(XmlAccessType.FIELD)
public class Step {
@XmlID
@XmlAttribute
private String _id;
}
route
@XmlAccessorType(XmlAccessType.FIELD)
public class Path {
@XmlIDREF
@XmlAttribute
private Step _reference;
}
For more information
> http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html
UPDATE
Note: I am the leader of eclipse link JAXB (Moxy) and a member of JAXB (jsr-222) expert group
If you use Moxy as a JAXB (jsr-222) provider, you can use 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
> http://bugs.eclipse.org/399293 > http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html > http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
