简体   繁体   English

JAXB到达嵌套XmlElement示例

[英]JAXB Reach Nested XmlElement Example

Here my XmlRoot class: 这是我的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; } } 

Then successor(child) class of root : 然后是root的后继(子)类:

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

At last, successor class of successor of root : 最后,root的继承者的继承者类:

 @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; } 

Myhandler class: Myhandler类:

 @Override public void handleXMLtoIABData(RequestTcmbXMLData req) throws HmnServiceException { try { JAXBContext jaxbContext = JAXBContext.newInstance(IGEType.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); File XMLfile = new File("C:\\\\Users\\\\U067944\\\\Desktop\\\\IAB_bülten.xml"); IGEType igeRoot = (IGEType) jaxbUnmarshaller.unmarshal(XMLfile); **List<SEANSytlType> listofAltinYtl = (List<SEANSytlType>) ((List<ALTINType>) igeRoot.getALTIN()).getSEANSytl();** for (SEANSytlType altinYtl : listofAltinYtl) { } } catch (JAXBException e) { e.printStackTrace(); } } 

In my handler class I try to reach last successor class (List SEANSytlType ), but it doesnt work. 在我的处理程序类中,我尝试到达最后一个后继类(列表SEANSytlType),但是它不起作用。 I get this error : 我收到此错误:

jvmId: [300], transactionId:[3005624292568000] .Root Cause: [java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.ykb.hmn.mdt.marketdata.xmlparser.iab.ALTINType] jvmId:[300],transactionId:[3005624292568000]。根本原因:[java.lang.ClassCastException:java.util.ArrayList无法转换为com.ykb.hmn.mdt.marketdata.xmlparser.iab.ALTINType]

I also try this in handler but same: 我也尝试在处理程序,但相同的:

IGEType igeRoot = (IGEType) jaxbUnmarshaller.unmarshal(XMLfile);

            String inputDate = igeRoot.getIGEBULTENGUNTR().getGun2();
             List<ALTINType> listAltinRoot = (List<ALTINType>) igeRoot.getALTIN();
             List<SEANSytlType> listofAltinYtl = (List<SEANSytlType>) listAltinRoot.get(0);

Where am I wrong? 我哪里错了? Thanks in advance! 提前致谢!

Based your sample classes IGEType 基于您的样本类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;
    }
}

Then ALTINType 然后ALTINType

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

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

}

Then SEANSytlType 然后是SEANSytlType

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

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

Then sample class for test 然后进行样品测试

 <!-- 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();
    }
}

So and and end sample xml 因此,并结束示例xml

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

</IGE>

So basicly problem was that you have in root (IGE) childs list altin but in altin object thest is only 1 child object seansylt not at list like you have in altin so fix altin object add list there and take look at my example 所以基本上的问题是,您在根(IGE)子列表中有altin,但在altin对象中,只有1个子对象seansylt不在列表中,就像您在altin中一样,因此修复altin对象在此添加列表并看一下我的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM