Question

I have different XML returns that I want to parse with JAXB with annotated classes. Many of the XML files share a similar top structure and with the contents of an inner tag that can vary. Since we sent the ContextInstance a class and not an instance I can't add classes.

How would you create a set of JAXB annotations that don't repeat the top level xml each time, given examples of two files like this.

<?xml version='1.0' encoding='UTF-8'?>
<eveapi version="2">
  <currentTime>2008-11-24 20:14:29</currentTime>
  <result>
    <serverOpen>True</serverOpen>
    <onlinePlayers>38102</onlinePlayers>
  </result>
  <cachedUntil>2008-11-24 20:17:29</cachedUntil>
</eveapi>

And another file like this:

<?xml version='1.0' encoding='UTF-8'?>
<eveapi version="2">
  <currentTime>2012-02-12 14:39:12</currentTime>
  <result>
    <rowset name="characters" key="characterID"     columns="name,characterID,corporationName,corporationID">
      <row name="Alexis Prey" characterID="1365215823" corporationName="Puppies To the     Rescue" corporationID="238510404"/>
    </rowset>
  </result>
  <cachedUntil>2012-02-12 15:27:00</cachedUntil>
</eveapi>

A common top level class would be something like

@XmlRootElement(name="eveapi")
public class EveApiConfig {
    @XmlElement(required = true)
    protected Date currentTime;

    @XmlElement(required = true)
    protected String cachedUntil;

    @XmlAttribute(name = "version", required = true)
    protected int version;

    @XmlElement(required = true)
    protected ResultsConfig result;
}

But ResultsConfig would be number of other things. it's almost like a need a reverse XmlSeeAlso

Any thoughts if how to do this?

Was it helpful?

Solution

I think you can achieve this with several object factories. I'll sketch out the solution:

  • Make result an @XmlElementRef, typed with something like JAXBElement<AbstractResultConfig>
  • Per case create one package with an ObjectFactory
  • In which of the ObjectFactory classes implement a different @XmlElementDecl factory method which would create JAXBElements with instance of different subclasses of the AbstractResultConfig. Something like:

    @XmlElementDecl(name = "Result")
    public JAXBElement<ResultConfigImpl> createResultConfigImpl(ResultConfigImpl value) {
        return new JAXBElement<ResultConfigImpl>(..., ResultConfigImpl.class, null, value);
    }
    

I guess you'll need to pair ith with createResultConfig() method or something like this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top