Issue
I have a set of XML files, which contain different information but all have the same root tag. The tags look like this:
First file:
<node type="A"> ... </node>
Second file:
<node type="B"> ... </node>
and so forth. Now the question is: can I use an annotation expression like this to have JAXB serialise them into different types:
A.java
@XmlRootElement(name = "node[@type='A']/text()")
public class A { ... }
B.java
@XmlRootElement(name = "node[@type='B']/text()")
public class A { ... }
Is this possible?
Solution
What you are trying isn't possible how you are trying to do it.
You could parse the XML with a StAX XMLStreamReader
, peek at the attributes on the root element event, choose which class to unmarshal, and then call the unmarshal
method that takes a Class
and an XMLStreamReader
as parameters.
Answered By - bdoughan
Answer Checked By - Marie Seifert (JavaFixing Admin)