Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a Value for JAXBElement<String>

Tags:

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.

I have an ObjectFactory, but it only creates the class 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);     } } 
like image 662
EdgeCase Avatar asked Oct 23 '13 17:10

EdgeCase


People also ask

How do you set the string value on a JAXBElement?

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.

How do I initialize JAXBElement?

Code example extracted from Stack Overflow: ObjectFactory factory = new ObjectFactory(); JAXBElement<String> createMessageDescription = factory. createMessageDescription("description"); message. setDescription(createMessageDescription);

What is QName in JAXBElement?

QName getName() This method returns the xml element tag name. 3. Class getScope() This method returns scope of xml element declaration.


1 Answers

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 objectFactory = new ObjectFactory(); JAXBElement<String> jaxbElement = objectFactory.createAmount("Hello World"); 

UPDATE

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

like image 115
bdoughan Avatar answered Sep 23 '22 08:09

bdoughan