Jürgen Simon
Jürgen Simon

Reputation: 896

Eclipse MOXy : @XMLRootElement based on attribute - possible?

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?

Upvotes: 1

Views: 141

Answers (1)

bdoughan
bdoughan

Reputation: 149047

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.

Upvotes: 1

Related Questions