2011-02-03 40 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>(); 

} 
相關問題