Reputation: 571
I have to initialize an element of type JAXBElement <String>
, I have tried as follows:
JAXBElement<String> element = new JAXBElement<>(new QName("http://tempuri.org/", "FieldName"), String.class, "FieldData");
But I am not sure if it is the correct way. Can someone confirm if there is another easier way?
Upvotes: 2
Views: 1336
Reputation: 300
What you posted is the simplest way of initializing a JAXBElement
element that I know of - a correct one.
The two constructors are:
JAXBElement(QName name, Class<T> declaredType, Class scope, T value)
and (the simplest one, the one you used)
JAXBElement(QName name, Class<T> declaredType, T value)
Also, keep in mind that if by Simple you mean, you don't need to initialize the object with the scope (3rd argument of the first constructor), then your code should be fine.
Edit:
The only questionable thing I see is the "FieldName"
(2nd argument provided to the QName
constructor) - I'm not what it represents for you, but this should be the local part of the QName
. For more info about this see
public QName(String namespaceURI, String localPart)
Upvotes: 1