为JAXBElement< String>设置值 [英] Set a Value for JAXBElement<String>

查看:234
本文介绍了为JAXBElement< String>设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成的类,如下所示.我需要从POJO调用setAmount(),但我不知道要为arg传递什么值.它需要使用JAXBElement类型,但我还没有找到实例化该方法的方法.

I have a generated class that looks like below. I need to call setAmount() from a POJO, but I don't know what value to pass for the arg. It takes type JAXBElement, and I haven't found a way to instantiate that.

我有一个ObjectFactory,但它只创建CardRequest类.

I have an ObjectFactory, but it only creates the class CardRequest.

有人可以提出建议吗?

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "amount",
})
@XmlRootElement(name = "card-request")
public class CardRequest {

    @XmlElementRef(name = "amount", namespace = "http://mycompany/services", type = JAXBElement.class)
    protected JAXBElement<String> amount;

    public JAXBElement<String> getAmount() {
        return amount;
    }

    public void setAmount(JAXBElement<String> value) {
        this.amount = ((JAXBElement<String> ) value);
    }
}

推荐答案

您可以执行以下操作:

JAXBElement<String> jaxbElement = 
    new JAXBElement(new QName("http://mycompany/services", "amount"), String.class, "Hello World");

在生成的ObjectFactory类上还应该有一个create方法,该方法将为您创建带有适当信息的JAXBElement实例.

There should also be a create method on the generated ObjectFactory class that will create this instance of JAXBElement with the appropriate info for you.

ObjectFactory objectFactory = new ObjectFactory();
JAXBElement<String> jaxbElement = objectFactory.createAmount("Hello World");


更新

如果元素定义嵌套在您的模式中,则create方法的名称可能会更长,例如createCardRequestAmount().


UPDATE

If the element definition is nested within your schema the name of the create method might be longer such as createCardRequestAmount().

这篇关于为JAXBElement&lt; String&gt;设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆