2011-02-03 42 views
5

我正在从Castor切换到JAXB2以在XML和Java对象之间执行封送/解组。我在尝试配置多态对象集合时遇到了问题。将多态对象的集合配置为在JAXB2中工作

示例XML

<project name="test project"> 
    <orange name="fruit orange" orangeKey="100" /> 
    <apple name="fruit apple" appleKey="200" /> 
    <orange name="fruit orange again" orangeKey="500" /> 
</project> 

工程类

oranges表工作正常,我看到列表中的2个橘子。但是,我不知道如何配置fruitListfruitList应该有3个水果:2个橙子和1个苹果。

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

    @XmlAttribute 
    private String   name; 

    @XmlElement(name = "orange") 
    private List<Orange> oranges  = new ArrayList<Orange>(); 

    // Not sure how to configure this... help! 
    private List<Fruit>  fruitList = new ArrayList<Fruit>(); 
} 

水果类

果是一个抽象类。出于某种原因,将这个类定义为抽象似乎导致了很多问题。

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public abstract class Fruit { 

    @XmlAttribute 
    private String name; 
} 

橙类

public class Orange extends Fruit { 

    @XmlAttribute 
    private String orangeKey; 
} 

苹果类

public class Apple extends Fruit { 

    @XmlAttribute 
    private String appleKey; 
} 

如何将我的fruitListProject配置达到我想要的吗?

非常感谢!

回答

4

你想利用@XmlElementRef这相当于取代基的XML架构的概念,它对应于你的问题。

第1步 - 使用@XmlElementRef

的fruitList属性标注有@XmlElementRef:

import java.util.ArrayList; 
import java.util.List; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElementRef; 
import javax.xml.bind.annotation.XmlRootElement; 

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

    @XmlAttribute 
    private String name; 

    @XmlElementRef 
    private List<Fruit> fruitList = new ArrayList<Fruit>(); 

} 

第2步 - 注释苹果与橘子同@XmlRootElement

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Apple extends Fruit { 

    @XmlAttribute 
    private String appleKey; 

} 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Orange extends Fruit { 

    @XmlAttribute 
    private String orangeKey; 

} 

演示代码

以下代码可被用于证明溶液:

import java.io.File; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.Unmarshaller; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(Project.class, Apple.class, Orange.class); 

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     Project project = (Project) unmarshaller.unmarshal(new File("input.xml")); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(project, System.out); 
    } 

} 

更多信息:

+0

我尝试了您的方法...我注释了`fruitList` `@ XmlElementRef`属性和'@ XmlRootElement`注释`Orange`和`Apple`类。我运行了代码,`fruitList`空了。我在这里做错了什么?谢谢。 – limc 2011-02-03 16:53:15

0

认沽XmlAnyElement将注释:

@XmlAnyElement(lax = true) 
private List<Fruit>  fruitList = new ArrayList<Fruit>(); 
+0

这并不工作...`fruitList`包含一个`apple`类型的`ElementNSImpl`。 – limc 2011-02-03 16:08:14

+0

对于@XmlAnyElement,您需要为每个子类添加@XmlRootElement。尽管这对于通配符部分来说意味着更多。由于子对象处于继承关系中@XmlElementRef是一个更好的选择。有关@XmlAnyElement的更多信息,请参阅:http://bdoughan.blogspot.com/2010/08/using-xmlanyelement-to-build-generic.html – 2011-02-03 16:19:07

2

把玩后,周围......我想我现在的工作: -

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

    @XmlAttribute 
    private String  name; 

    // this has to be commented out, or else oranges won't show up in fruitList 
    // @XmlElement(name = "orange") 
    // private List<Orange> oranges = new ArrayList<Orange>(); 

    @XmlElements({ 
      @XmlElement(name = "orange", type = Orange.class), 
      @XmlElement(name = "apple", type = Apple.class) 
    }) 
    private List<Fruit> fruitList = new ArrayList<Fruit>(); 

} 
相关问题