为JAXBElement< String>设置值 [英] Set a Value for 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< String>设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!