JAXB: how to unmarshal a List of objects of different types but with common parent?

萝らか妹 提交于 2019-12-17 19:15:20

问题


There is a fairly common pattern in our applications. We configure a configure a set (or list) of objects in Xml, which all implement a common interface. On start-up, the application reads the Xml and uses JAXB to create/configure a List of objects. I have never figured out (after reading various posts many times) the "right way" to do this using only JAXB.

For example, we have an interface Fee, and multiple concrete implementing classes which have some common properties, as well as some diverging properties, and very different behaviors. The Xml we use to configure the List of Fees used by application is:

<fees>
   <fee type="Commission" name="commission" rate="0.000125" />
   <fee type="FINRAPerShare" name="FINRA" rate="0.000119" />
   <fee type="SEC" name="SEC" rate="0.0000224" />
   <fee type="Route" name="ROUTES">
       <routes>
        <route>
            <name>NYSE</name>
            <rates>
                <billing code="2" rate="-.0014" normalized="A" />
                <billing code="1" rate=".0029" normalized="R" />
            </rates>
        </route>        
        </routes>
          ...
    </fee>
  </fees>

In the above Xml, each <fee> element corresponds to a concrete subclass of a Fee interface. The type attribute gives information about which type to instantiate, and then once it is instantiated, the JAXB unmarshalling applies the properties from the remaining Xml.

I always have to resort to doing something like this:

private void addFees(TradeFeeCalculator calculator) throws Exception {
    NodeList feeElements = configDocument.getElementsByTagName("fee");
    for (int i = 0; i < feeElements.getLength(); i++) {
        Element feeElement = (Element) feeElements.item(i);
        TradeFee fee = createFee(feeElement);
        calculator.add(fee);
    }
}

private TradeFee createFee(Element feeElement) {
    try {
        String type = feeElement.getAttribute("type");
        LOG.info("createFee(): creating TradeFee for type=" + type);
        Class<?> clazz = getClassFromType(type);
        TradeFee fee = (TradeFee) JAXBConfigurator.createAndConfigure(clazz, feeElement);
        return fee;
    } catch (Exception e) {
        throw new RuntimeException("Trade Fees are misconfigured, xml which caused this=" + XmlUtils.toString(feeElement), e);
    }
}

In the above code, the JAXBConfigurator is just a simple wrapper around the JAXB objects for unmarshalling:

public static Object createAndConfigure(Class<?> clazz, Node startNode) {
    try {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        @SuppressWarnings("rawtypes")
        JAXBElement configElement = unmarshaller.unmarshal(startNode, clazz);
        return configElement.getValue();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

At the end, of the above code, we get a List which contains whichever types were configured in the Xml.

Is there a way to get JAXB to do this automatically without having to write the code to iterate the elements as above?


回答1:


Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

If you are using MOXy as your JAXB provider then you could use the MOXy's @XmlPaths annotation to extend the standard JAXB @XmlElements annotation to do the following:

Fees

import java.util.List;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.*;

@XmlRootElement
public class Fees {

    @XmlElements({
        @XmlElement(type=Commission.class),
        @XmlElement(type=FINRAPerShare.class),
        @XmlElement(type=SEC.class),
        @XmlElement(type=Route.class)
    })
    @XmlPaths({
        @XmlPath("fee[@type='Commission']"),
        @XmlPath("fee[@type='FINRAPerShare']"),
        @XmlPath("fee[@type='SEC']"),
        @XmlPath("fee[@type='Route']")
    })
    private List<Fee> fees;

}

Commission

The implementations of the Fee interface would be annotated normally.

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Commission implements Fee {

    @XmlAttribute
    private String name;

    @XmlAttribute
    private String rate;

}

For More Information

  • http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html
  • http://blog.bdoughan.com/2010/10/jaxb-and-xsd-choice-xmlelements.html
  • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html



回答2:


You could use an XmlAdapter for this use case. The impl bleow handles just the Commission type but could be easily extended to support all the types. You need to ensure that AdaptedFee contains the combined properties from all the implementations of the Fee interface.

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class FeeAdapter extends XmlAdapter<FeeAdapter.AdaptedFee, Fee>{

    public static class AdaptedFee {

        @XmlAttribute
        public String type;

        @XmlAttribute
        public String name;

        @XmlAttribute
        public String rate;

    }

    @Override
    public AdaptedFee marshal(Fee fee) throws Exception {
        AdaptedFee adaptedFee = new AdaptedFee();
        if(fee instanceof Commission) {
            Commission commission = (Commission) fee;
            adaptedFee.type = "Commission";
            adaptedFee.name = commission.name;
            adaptedFee.rate = commission.rate;
        }
        return adaptedFee;
    }

    @Override
    public Fee unmarshal(AdaptedFee adaptedFee) throws Exception {
        if("Commission".equals(adaptedFee.type)) {
            Commission commission = new Commission();
            commission.name = adaptedFee.name;
            commission.rate = adaptedFee.rate;
            return commission;
        }
        return null;
    }

}

An XmlAdapter is configured using the @XmlJavaTypeAdapter annotation:

import java.util.List;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Fees {

    @XmlElement(name="fee")
    @XmlJavaTypeAdapter(FeeAdapter.class)
    private List<Fee> fees;

}

For More Information

  • http://blog.bdoughan.com/2012/01/jaxb-and-inhertiance-using-xmladapter.html



回答3:


I don't think this is possible if all the elements are named <fee>. Even if it were(or is) it would be very confusing from maintenance point of view.

Do you have the ability to rename various fee elements based on type (e.g. <tradeFee> instead of <fee>)?

Otherwise you can create a BaseFee class that has all the fields for every possible type of <fee>. You can unmarshall data into a list of BaseFee objects and convert them into a more specific type at runtime, e.g.

List<BaseFee> fees = ...;
for (BaseFee fee : fees) {
    if (isTradeFee(fee)) {
        TradeFee tradeFee = toTradeFee(fee);
        // do something with trade fee...
    }
}

A bit of a hack but given the requirements it should do the job.



来源:https://stackoverflow.com/questions/15078566/jaxb-how-to-unmarshal-a-list-of-objects-of-different-types-but-with-common-pare

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!