加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 08:49:55 所属栏目:Java 来源:网络整理
导读:考虑以下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 _referenc
考虑以下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;
    }

}

我不想将引用作为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>

以下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对象的情况下创建链接?

再次感谢 : )!

解决方法

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

@XmlAccessorType(XmlAccessType.FIELD)
public class Step {

    @XmlID
    @XmlAttribute
    private String _id;

}

路径

@XmlAccessorType(XmlAccessType.FIELD)
public class Path {

    @XmlIDREF
    @XmlAttribute
    private Step _reference;

}

欲获得更多信息

> http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html

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.

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

}

欲获得更多信息

> 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读