JAXB:如何解组不同类型但具有共同父级的对象列表? [英] JAXB: how to unmarshal a List of objects of different types but with common parent?

查看:187
本文介绍了JAXB:如何解组不同类型但具有共同父级的对象列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用程序中有一个相当常见的模式。我们在Xml中配置一个对象的集合(或列表),它们都实现了一个公共接口。在启动时,应用程序读取Xml并使用JAXB创建/配置对象列表。我从未想过(经过多次阅读各种帖子后)只使用JAXB的正确方法。

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.

例如,我们有一个接口费用,以及具有一些共同属性的多个具体实现类作为一些不同的属性,和非常不同的行为。我们用来配置应用程序使用的费用列表的Xml是:

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>

在上面的Xml中,每个< fee> element对应Fee接口的具体子类。 类型属性提供有关要实例化的类型的信息,然后在实例化后,JAXB解组将应用剩余Xml中的属性。

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);
    }
}

在上面的代码中, JAXBConfigurator 只是用于解组的JAXB对象的简单包装器:

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);
    }
}

最后,在上面的代码中,我们得到包含在Xml中配置的任何类型的List。

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

有没有办法让JAXB自动执行此操作而无需编写代码来迭代上述元素?

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

推荐答案

注意:我是 EclipseLink JAXB(MOXy) 领导和 JAXB(JSR-222) 专家组。

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

如果您使用MOXy作为JAXB提供程序,那么您可以使用MOXy的 @XmlPaths 注释来扩展标准JAXB @XmlElements 注释,以执行以下操作:

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:

费用

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;

}

佣金

费用界面的实现将正常注释。

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;

}

更多信息

  • 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

这篇关于JAXB:如何解组不同类型但具有共同父级的对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆