2013-11-28 5 views
0

Вот мой класс XmlRoot:JAXB Reach Уплотненный XmlElement Пример

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

Тогда преемник (ребенок) класс корня:

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

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

} 

наконец, преемник класса преемника корня:

@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 класс:

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

В моем классе обработчика я пытаюсь достичь последнего преемника класса (Список SEANSytlType), но он не работает. я получаю эту ошибку:

jvmId: [300], TransactionID: [3005624292568000] .Root Причина: [java.lang.ClassCastException: java.util.ArrayList не может быть приведен к com.ykb.hmn.mdt. marketdata.xmlparser.iab.ALTINType]

Я также попробовать это в обработчике, но то же самое:

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); 

Где я ошибаюсь? Спасибо заранее!

ответ

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> 

Так основно проблема заключалась в том, что у вас есть в корне (МГЭ) порожденный список ALTIN ​​но в ALTIN ​​объект thest только один дочерний объект seansylt не в списке, как у вас есть в altin, так что исправьте объект altin, добавьте список и посмотрите мой пример

+0

Спасибо за объяснение и ответ –

Смежные вопросы