编组具有对象字段的对象

发布于 2024-08-18 15:02:24 字数 610 浏览 3 评论 0原文

不确定标题是否有意义。 我想使用 JAXB 封送一个如下所示的对象:

@XmlRootElement(name = "subscriptionRequest")
    public class RegistrationRequest {

    private Long id;
    private RegistrationSource registrationSource;
    }

RegistrationSource 对象:

 public class RegistrationSource {

    private Integer id;
    private String code;
}

我想创建一个具有以下布局的 xml:

<subscriptionRequest registrationSource="0002">
    ...
</subscriptionRequest>

其中,registrationSource 属性值是来自 RegistrationSource 对象的代码字段值。

我需要使用哪些 xml 注释?

Not sure if the title makes any sense.
I have an object that I want to marshal using JAXB that looks like this:

@XmlRootElement(name = "subscriptionRequest")
    public class RegistrationRequest {

    private Long id;
    private RegistrationSource registrationSource;
    }

The RegistrationSource object:

 public class RegistrationSource {

    private Integer id;
    private String code;
}

I want to create an xml that has the following layout:

<subscriptionRequest registrationSource="0002">
    ...
</subscriptionRequest>

where the registrationSource attribute value is the code field value from the RegistrationSource object.

What xml annotations do I need to use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-08-25 15:02:24

registrationSource 上的 @XmlAttributecode 上的 @XmlValue。请注意,在这种情况下,您还应该在 RegistrationSource 的其他字段上使用 @XmlTransient,例如 id

编辑:这有效:

@XmlRootElement(name = "subscriptionRequest") 
public class RegistrationRequest { 

    private Long id; 
    private RegistrationSource registrationSource; 

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    @XmlAttribute
    public RegistrationSource getRegistrationSource() { return registrationSource; }
    public void setRegistrationSource(RegistrationSource registrationSource)
    {
        this.registrationSource = registrationSource;
    }
}

-

public class RegistrationSource { 

    private Integer id; 
    private String code; 

    @XmlTransient
    public Integer getId() { return id; }
    public void setId(Integer id) { this.id = id; }

    @XmlValue
    public String getCode() { return code; }
    public void setCode(String code) { this.code = code; }
}

@XmlAttribute on registrationSource, @XmlValue on code. Note that in this case you also should have @XmlTransient on other fields of RegistrationSource, such as id

EDIT: This works:

@XmlRootElement(name = "subscriptionRequest") 
public class RegistrationRequest { 

    private Long id; 
    private RegistrationSource registrationSource; 

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    @XmlAttribute
    public RegistrationSource getRegistrationSource() { return registrationSource; }
    public void setRegistrationSource(RegistrationSource registrationSource)
    {
        this.registrationSource = registrationSource;
    }
}

-

public class RegistrationSource { 

    private Integer id; 
    private String code; 

    @XmlTransient
    public Integer getId() { return id; }
    public void setId(Integer id) { this.id = id; }

    @XmlValue
    public String getCode() { return code; }
    public void setCode(String code) { this.code = code; }
}
雨夜星沙 2024-08-25 15:02:24

如果您想使用某些工具自动生成此类,请尝试以下操作 -
使用 Trang 等工具从 xml 生成 xsd,然后使用从 xsd 生成 java 文件jaxb。生活会简单得多:)

If you want to generate this class automatically using some tools , then try this -
Generate xsd from your xml using tools like Trang, and then generate java file from xsd using jaxb. Life would be much simpler :)

马蹄踏│碎落叶 2024-08-25 15:02:24

蹩脚的方法是

@XmlAttribute(name = "registrationSource")
private String getCode() {
    return registrationSource.code;
}

向您的 RegistrationSource 添加类似的内容 - 但必须有一种更优雅的方法......

The lame approach would be to add something like

@XmlAttribute(name = "registrationSource")
private String getCode() {
    return registrationSource.code;
}

to your RegistrationSource -- but there must be a more elegant way...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文