使用JAXB根据属性创建引用对象

Vel*_*lth 3 java xml jaxb

考虑以下xml:

<Config>
    <Paths>
        <Path reference="WS_License"/>
    </Paths>

    <Steps>
        <Step id="WS_License" title="License Agreement" />
    </Steps>
</Config>
Run Code Online (Sandbox Code Playgroud)

以下JAXB类:

public class Path {

    private String _reference;

    public String getReference() {
        return _reference;
    }

    @XmlAttribute
    public void setReference( String reference ) {
        _reference = reference;
    }

}
Run Code Online (Sandbox Code Playgroud)

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

我不想将引用作为String存储在Path对象中,而是将其作为Step对象保存.这些对象之间的链接是reference和id属性.@XMLJavaTypeAdapter属性是否可行?任何人都可以如此善良地提供正确用法的例子吗?

谢谢!

编辑:

我也想用元素做同样的技术.

考虑以下xml:

<Config>
    <Step id="WS_License" title="License Agreement">
        <DetailPanelReference reference="DP_License" />
    </Step>

    <DetailPanels>
        <DetalPanel id="DP_License" title="License Agreement" />
    </DetailPanels>
</Config>
Run Code Online (Sandbox Code Playgroud)

以下JAXB类:

@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;

}
Run Code Online (Sandbox Code Playgroud)

Step-object中的属性_detailPanels为空,链接似乎不起作用.是否有任何选项可以在不创建仅包含对DetailPanel的引用的新JAXB对象的情况下创建链接?

再次感谢 : )!

bdo*_*han 7

您可以使用@XmlID映射属性作为键,@XmlIDREF并将引用映射到此用例的键.

@XmlAccessorType(XmlAccessType.FIELD)
public class Step {

    @XmlID
    @XmlAttribute
    private String _id;

}
Run Code Online (Sandbox Code Playgroud)

路径

@XmlAccessorType(XmlAccessType.FIELD)
public class Path {

    @XmlIDREF
    @XmlAttribute
    private Step _reference;

}
Run Code Online (Sandbox Code Playgroud)

欲获得更多信息


UPDATE

谢谢!我完全错过了你的文章.我已经扩展了我的问题,如果可能的话,你有任何线索吗?我不想创建只包含引用的类,我想将它存储在步骤类中.

注意: 我是EclipseLink JAXB(MOXy)的负责人,也是JAXB(JSR-222)专家组的成员.

如果您使用MOXy作为JAXB(JSR-222)提供程序,那么您可以将@XmlPath注释用于您的用例.

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

}
Run Code Online (Sandbox Code Playgroud)

欲获得更多信息