Java JAXB Reach嵌套XmlElement示例
下面是我的XmlRoot类:Java JAXB Reach嵌套XmlElement示例,java,jaxb,annotations,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>
@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;
}
@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();
}
}
最后,根的继承者的继承者类:
@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;
}
@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();
}
}
Myhandler类:
@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;
}
@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();
}
}
@覆盖
public void handlexmltoabdata(RequestTcmbXMLData req)引发HmnServiceException{
试一试{
JAXBContext JAXBContext=JAXBContext.newInstance(IGEType.class);
解组器jaxbUnmarshaller=jaxbContext.createUnmarshaller();
File XMLfile=新文件(“C:\\Users\\U067944\\Desktop\\IAB_ülten.xml”);
IGEType IGEOOT=(IGEType)jaxbUnmarshaller.unmarshal(XMLfile);
**List listofAltinYtl=(List)((List)IGEOOT.getALTIN()).getSEANSytl()**
对于(SEANSytlType altinYtl:listofAltinYtl){
}
}捕获(JAXBEException e){
e、 printStackTrace();
}
}
在我的处理程序类中,我试图到达最后一个后续类(List SEANSytlType),但它不起作用。
我得到这个错误:
jvmId:[300],transactionId:[3005624292568000]。根本原因:[java.lang.ClassCastException:java.util.ArrayList无法强制转换为com.ykb.hmn.mdt.marketdata.xmlparser.iab.ALTINType]
我也在handler中尝试了这一点,但相同:
IGEType IGEOOT=(IGEType)jaxbUnmarshaller.unmarshal(XMLfile);
字符串inputDate=IGEOOT.GetIgebultengunt().getGun2();
List listAltinRoot=(List)igeRoot.getALTIN();
List listofAltinYtl=(List)listAltinRoot.get(0);
我错在哪里?
提前谢谢 基于示例类
IGEType
然后是SEANSytlType
<!-- language: java -->
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SEANSytl", propOrder = { "birim"})
public class SEANSytlType {
@XmlElement(required = true)
protected String birim;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name=“SEANSytl”,proporter={“birim”})
公共类SEANSytlType{
@XmlElement(必需=true)
受保护字符串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();
}
}
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
试一试{
JAXBContext JAXBContext=JAXBContext.newInstance(IGEType.class);
解组器jaxbUnmarshaller=jaxbContext.createUnmarshaller();
File XMLfile=新文件(“sample1.xml”);
IGEType IGEOOT=(IGEType)jaxbUnmarshaller.unmarshal(XMLfile);
List listAltinRoot=igeRoot.getALTIN();
//这是你的错误///
//List listofAltinYtl=(List)listAltinRoot.get(0);
//
//对于(SEANSytlType altinYtl:listofAltinYtl){
//System.out.println(altinYtl.birim);
// }
}捕获(JAXBEException 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>
中交
dddd
所以最基本的问题是,在根目录(IGE)中有子对象列表altin,但在altin对象中只有一个子对象seansylt不像在altin中那样在列表中,所以请修复altin对象添加列表并查看我的示例谢谢您的解释和回答