...">

Using JAXB to create referenced objects according to an attribute

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 a String, I would like to store it as a Step object. The relationship between these objects is reference and identification attributes. @XMLJavaTypeAdapter attribute - path? Can anyone be kind enough to provide an example of correct usage?

Thank!

EDIT:

I would also 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 _detailPanels property in the Step object is empty and the link does not work. Is it possible to create a link without creating a new JAXB object containing only a link to the DetailPanel?

Thanks again:)!

+3


source to share


1 answer


You can use @XmlID

to match a property as a key and @XmlIDREF

to match a key reference for this use case.

Step

@XmlAccessorType(XmlAccessType.FIELD)
public class Step {

    @XmlID
    @XmlAttribute
    private String _id;

}

      

Way

@XmlAccessorType(XmlAccessType.FIELD)
public class Path {

    @XmlIDREF
    @XmlAttribute
    private Step _reference;

}

      

Additional Information




UPDATE

Thank! I completely missed your article. I expanded on my question, do you have any hints if possible? I don't want to create a class that only contains a reference, I would like to store it inside a step class.

Note. I am EclipseLink JAXB (MOXy) and a member of the JAXB Expert Group (JSR-222) .

If you are using MOXy as your JAXB provider (JSR-222) then you can use annotation @XmlPath

for your use.

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

}

      

Additional Information

+7


source







All Articles