2011-04-25 71 views
4

我想知道是否有可能將xml元素解組到多個pojos。例如:JAXB unmarshall到多個pojo的

用於XML:

<type> 
    <id>1</id> 
    <cost>12</cost> 
    <height>15</height> 
    <width>13</width> 
    <depth>77</depth> 
</type> 

Item類

@XmlAccessorType(XmlAccessType.PROPERTY) 
@XmlRootElement(name="type") 
public class Item { 
    private Integer id; 
    private Double cost; 

    @XmlElement(name="id") 
    public Integer getId(){ 
    return id; 
    } 

    @XmlElement(name="cost") 
    public Double getCost(){ 
    return cost 
    } 
} 

ItemDimensions類

@XmlAccessorType(XmlAccessType.PROPERTY) 
@XmlRootElement(name="type") 
public class ItemDimensions { 
    private Integer height; 
    private Integer width; 
    private Integer depth; 

    @XmlElement(name="height") 
    public Integer getHeight(){ 
    return height; 
    } 

    @XmlElement(name="width") 
    public Integer getWidth(){ 
    return width; 
    } 

    @XmlElement(name="depth") 
    public Integer getDepth(){ 
    return depth; 
    } 
} 

我曾嘗試使用許多由產生JAXB映射來完成類似的東西Netbeans 6.9和一些測試類,但現在已經得到了。有誰知道這是否可以在沒有任何中介對象的情況下完成?

+0

AFAIK您將不得不自己創建ItemDimensions對象。例如返回新的ItemDimensions(高度,寬度,深度)。 – nabeelmukhtar 2011-04-25 15:13:45

回答

2

您可以使用@XmlPath擴展在EclipseLink JAXB (MOXy)來完成這個用例(我是莫西技術主管):

JAXB需要一個對象來解組,我們將介紹一個班級來完成這個角色。這個類必須與您要散集與自我的XPath註釋兩個對象的字段:(「」)@XmlPath

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

import org.eclipse.persistence.oxm.annotations.XmlPath; 

@XmlRootElement(name="type") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Root { 

    @XmlPath(".") 
    private Item item; 

    @XmlPath(".") 
    private ItemDimensions itemDimensions; 

} 

ItemDimensions

您正常註釋這個類。在你的例子中你註釋了屬性,但只提供getters。這將導致JAXB認爲這些是隻寫映射。

import javax.xml.bind.annotation.*; 

@XmlAccessorType(XmlAccessType.FIELD) 
public class ItemDimensions { 

    private Integer height; 
    private Integer width; 
    private Integer depth; 

} 

項目

import javax.xml.bind.annotation.*; 

@XmlAccessorType(XmlAccessType.FIELD) 
public class Item { 

    private Integer id; 
    private Double cost; 

} 

演示

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(Root.class); 

     Unmarshaller u = jc.createUnmarshaller(); 
     Object o = u.unmarshal(new File("input.xml")); 

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

} 

jaxb.properties

要使用莫西爲您的JAXB實現,您必須提供一個名爲jaxb.properties的文件與您的域對象與以下條目:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 
+0

+1我不明白「註釋只寫屬性的getter」部分,你可以詳細說明還是重定向到鏈接? – ekeren 2011-04-25 15:51:45

+0

@ekeren - 如果使用PROPERTY訪問並且未提供setter,那麼在解組期間,JAXB實現將無法「設置」對象上的值。一個解決方案是提供一個setter,另一個是使用FIELD訪問。 – 2011-04-25 15:54:22

+0

+1剛剛我剛纔也是。 – andyb 2011-06-10 15:08:30