JAXB到达嵌套的XmlElement示例

时间:2013-11-28 07:45:34

标签: java jaxb annotations

这是我的XmlRoot类:

    @XmlRootElement(name = "IGE")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "IGEType", propOrder = { "altin" })
public class IGEType {


    @XmlElement(name = "ALTIN", required = true)
    protected List<ALTINType> altin;

    public List<ALTINType> getALTIN() {
        if (altin == null) {
            altin = new ArrayList<ALTINType>();
        }
        return this.altin;
    }
}

@XmlRootElement(name = "IGE") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "IGEType", propOrder = { "altin" }) public class IGEType { @XmlElement(name = "ALTIN", required = true) protected List<ALTINType> altin; public List<ALTINType> getALTIN() { if (altin == null) { altin = new ArrayList<ALTINType>(); } return this.altin; } }

然后是root的继承者(孩子)类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ALTINType", propOrder = { "seanSytl" })
public class ALTINType {

    @XmlElement(name = "SEANSytl", required = true)
    protected SEANSytlType seanSytl;

}

最后,root的后继类继承者:

@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "ALTINType", propOrder = { "seanSytl" }) public class ALTINType { @XmlElement(name = "SEANSytl", required = true) protected SEANSytlType seanSytl; }

Myhandler类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SEANSytlType", propOrder = { "birim", "oncekiKapanis", "enDusuk", "enYuksek", "kapanis", "agirlikliOrtalama", "islemHacmi", "islemMiktari", "bicim", "gram", "islemSayisi" })
public class SEANSytlType {

    @XmlElement(required = true)
    protected String birim;
    @XmlElement(name = "onceki_kapanis", required = true)
    protected BigDecimal oncekiKapanis;
    @XmlElement(name = "en_dusuk", required = true)
    protected BigDecimal enDusuk;
    @XmlElement(name = "en_yuksek", required = true)
    protected BigDecimal enYuksek;
    @XmlElement(required = true)
    protected BigDecimal kapanis;
    @XmlElement(name = "agirlikli_ortalama", required = true)
    protected BigDecimal agirlikliOrtalama;
    @XmlElement(name = "islem_hacmi", required = true)
    protected BigDecimal islemHacmi;
    @XmlElement(name = "islem_miktari", required = true)
    protected BigDecimal islemMiktari;
    @XmlElement(name = "BICIM", required = true)
    protected BigDecimal bicim;
    @XmlElement(name = "GRAM", required = true)
    protected BigDecimal gram;
    @XmlElement(name = "islem_sayisi")
    protected int islemSayisi;
}

@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "SEANSytlType", propOrder = { "birim", "oncekiKapanis", "enDusuk", "enYuksek", "kapanis", "agirlikliOrtalama", "islemHacmi", "islemMiktari", "bicim", "gram", "islemSayisi" }) public class SEANSytlType { @XmlElement(required = true) protected String birim; @XmlElement(name = "onceki_kapanis", required = true) protected BigDecimal oncekiKapanis; @XmlElement(name = "en_dusuk", required = true) protected BigDecimal enDusuk; @XmlElement(name = "en_yuksek", required = true) protected BigDecimal enYuksek; @XmlElement(required = true) protected BigDecimal kapanis; @XmlElement(name = "agirlikli_ortalama", required = true) protected BigDecimal agirlikliOrtalama; @XmlElement(name = "islem_hacmi", required = true) protected BigDecimal islemHacmi; @XmlElement(name = "islem_miktari", required = true) protected BigDecimal islemMiktari; @XmlElement(name = "BICIM", required = true) protected BigDecimal bicim; @XmlElement(name = "GRAM", required = true) protected BigDecimal gram; @XmlElement(name = "islem_sayisi") protected int islemSayisi; }

在我的处理程序类中,我尝试到达最后一个继承类(List SEANSytlType),但它不起作用。 我收到此错误:

jvmId:[300],transactionId:[3005624292568000] .Root原因:[java.lang.ClassCastException:java.util.ArrayList无法强制转换为com.ykb.hmn.mdt.marketdata.xmlparser.iab.ALTINType]

我也在处理程序中尝试这个但是同样的:

我哪里错了? 提前致谢!

1 个答案:

答案 0 :(得分:0)

根据您的示例课程      IGEType     

@XmlRootElement(name = "IGE")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "IGE", propOrder = { "altin" })
public class IGEType {


    @XmlElement(name = "Altin", required = true)
    public List<ALTINType> altin;

    public List<ALTINType> getALTIN() {
        if (altin == null) {
            altin = new ArrayList<ALTINType>();
        }
        return this.altin;
    }
}

然后是ALTINType     

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "altin", propOrder = { "seanSytl" })
public class ALTINType {

    @XmlElement(name = "SEANSytl", required = true)
    protected SEANSytlType seanSytl;

}

然后SEANSytlType

<!-- language: java -->      
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SEANSytl", propOrder = { "birim"})
public class SEANSytlType {

    @XmlElement(required = true)
    protected String birim;
}

然后为测试提供样本

 <!-- language: java --> 
 public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(IGEType.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        File XMLfile = new File("sample1.xml");
        IGEType igeRoot = (IGEType) jaxbUnmarshaller.unmarshal(XMLfile);

        List<ALTINType> listAltinRoot = igeRoot.getALTIN();

        // here you ll be have error ///
//            List<SEANSytlType> listofAltinYtl =  (List<SEANSytlType>)      listAltinRoot.get(0);
//
//            for (SEANSytlType altinYtl : listofAltinYtl) {
//              System.out.println(altinYtl.birim);
//            }

    } catch (JAXBException e) {

        e.printStackTrace();
    }
}

所以和结束样本xml

<?xml version="1.0" encoding="UTF-8"?>
<IGE>
    <Altin>
        <SEANSytl>
            <birim>cccc</birim>
        </SEANSytl>
    </Altin>
    <Altin>
            <SEANSytl>
            <birim>dddd</birim>
        </SEANSytl>
    </Altin>

</IGE>

所以基本上问题是你有root(IGE)子列表altin但是在altin对象中只有1个子对象seansylt不在列表中就像你在altin那样修复altin对象添加列表然后看看我的例子