java JAXB:如何解组不同类型但具有共同父级的对象列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15078566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAXB: how to unmarshal a List of objects of different types but with common parent?
提问by Sam Goldberg
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 中配置了一组(或列表)对象,它们都实现了一个公共接口。启动时,应用程序读取 Xml 并使用 JAXB 创建/配置对象列表。我从来没有想过(在多次阅读各种帖子之后)只使用 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:
例如,我们有一个 interfaceFee
和多个具体的实现类,这些类具有一些共同的属性,以及一些不同的属性和非常不同的行为。我们用来配置应用程序使用的费用列表的 Xml 是:
<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.
在上面的 Xml 中,每个<fee>
元素对应一个 Fee 接口的具体子类。该type
属性提供有关要实例化的类型的信息,然后一旦实例化,JAXB 解组就会应用剩余 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:
在上面的代码中,JAXBConfigurator
它只是用于解组 JAXB 对象的简单包装器:
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.
在上述代码的最后,我们得到一个 List,其中包含在 Xml 中配置的任何类型。
Is there a way to get JAXB to do this automatically without having to write the code to iterate the elements as above?
有没有办法让 JAXB 自动执行此操作,而无需编写代码来迭代上述元素?
采纳答案by bdoughan
Note:I'm the EclipseLink JAXB (MOXy)lead and a member of the JAXB (JSR-222)expert group.
注意:我是EclipseLink JAXB (MOXy) 的负责人,也是JAXB (JSR-222)专家组的成员。
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:
如果您使用 MOXy 作为您的 JAXB 提供程序,那么您可以使用 MOXy 的@XmlPaths
注释来扩展标准 JAXB@XmlElements
注释以执行以下操作:
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.
Fee
接口的实现将被正常注释。
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Commission implements Fee {
@XmlAttribute
private String name;
@XmlAttribute
private String rate;
}
For More Information
想要查询更多的信息
回答by bdoughan
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.
您可以将 anXmlAdapter
用于此用例。impl bleow 只处理Commission
类型,但可以轻松扩展以支持所有类型。您需要确保AdaptedFee
包含来自Fee
接口的所有实现的组合属性。
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:
AnXmlAdapter
是使用@XmlJavaTypeAdapter
注解配置的:
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
想要查询更多的信息
回答by armandino
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.
如果所有元素都被命名,我认为这是不可能的<fee>
。即使是(或现在),从维护的角度来看也会非常混乱。
Do you have the ability to rename various fee elements based on type (e.g. <tradeFee>
instead of <fee>
)?
您是否能够根据类型(例如<tradeFee>
代替<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.
否则,您可以创建一个BaseFee
包含每种可能类型的所有字段的类<fee>
。您可以将数据解组为BaseFee
对象列表,并在运行时将它们转换为更具体的类型,例如
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.
有点黑客,但考虑到它应该完成这项工作的要求。