ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-07-29 07:11:06  阅读:156  来源: 互联网

标签:java xml jaxb


考虑以下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

标签:java,xml,jaxb
来源: https://codeday.me/bug/20190729/1569291.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有