I am trying to use JAXB to parse the following XML. I removed irrelevant parts. Unfortunately the original XML is generated by a third party application and I do not have the DTD or an XSD file available, so I am building my JAXB code by hand.
<add>
<add-attr attr-name="UserName">
<value type="string">User1</value>
</add-attr>
<add-attr attr-name="Name">
<value type="structured">
<component name="familyName">Doe</component>
<component name="givenName">John</component>
</value>
</add-attr>
</add>
The problem is of course the <value> element. This can be a element with plain text, or if its attribute type is “structured, a list of <component> elements.
I have created two classes (value1 and value2) which implement the two options, but I cannot tell JAXB which one to use, because the elements are both named “value”. Is there any solution?
Option #1 – One Value Class
You could have one
Valueclass that includes aStringproperty annotated with@XmlMixed.Option #2 – Multiple value Classes Via
XmlAdapterIf you want them to be separate classes you could leverage an
XmlAdapterfor this:ValueAdapter
AddAttr
For More Information