3

考虑以下 xml:

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

    <Steps>
        <Step id="WS_License" title="License Agreement" />
    </Steps>
</Config>

以下 JAXB 类:

public class Path {

    private String _reference;

    public String getReference() {
        return _reference;
    }

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

}

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

}

我不想将 Path 对象中的引用存储为 String,而是将其保存为 Step 对象。这些对象之间的链接是引用和 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>

以下 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;

}

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

再次感谢 : )!

4

1 回答 1

7

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

@XmlAccessorType(XmlAccessType.FIELD)
public class Step {

    @XmlID
    @XmlAttribute
    private String _id;

}

小路

@XmlAccessorType(XmlAccessType.FIELD)
public class Path {

    @XmlIDREF
    @XmlAttribute
    private Step _reference;

}

了解更多信息


更新

谢谢!我完全错过了你的文章。我已经扩展了我的问题,如果这也可能,您是否有任何线索?我不想创建一个只保存引用的类,我想将它存储在 step 类中。

注意: 我是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

}

了解更多信息

于 2013-01-28T09:39:22.663 回答