简体   繁体   English

为JAXBElement <String>设置值

[英]Set a Value for JAXBElement<String>

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. 我需要从POJO调用setAmount(),但我不知道为arg传递什么值。 It takes type JAXBElement, and I haven't found a way to instantiate that. 它需要类型JAXBElement,我还没有找到实例化它的方法。

I have an ObjectFactory, but it only creates the class CardRequest. 我有一个ObjectFactory,但它只创建了CardRequest类。

Can anyone suggest a way? 谁能提出建议?

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

You can do the following: 您可以执行以下操作:

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

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类上还应该有一个create方法,它将为您创建具有适当信息的JAXBElement实例。

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

UPDATE UPDATE

If the element definition is nested within your schema the name of the create method might be longer such as createCardRequestAmount() . 如果元素定义嵌套在模式中,则create方法的名称可能更长,例如createCardRequestAmount()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM