JAXB: class exception thrown when trying unmarshall XML using JAXB

I am new to JAXB, I am trying to use JAXB.

trying to calculate values ​​in MenuList.xml

---- MenuList.xml -----------

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<menulist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Category1 name="Development">
        <MenuItem1>Projects</MenuItem1>
        <MenuItem2>Library</MenuItem2>
        <MenuItem3>Library1</MenuItem3>
    </Category1>    
</menulist>

---------------------------- MenuList.xsd ---------------- ---

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio Developer Edition (Trial) 8.0.11.2171 (http://www.liquid-technologies.com)-->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="menulist">
        <xs:complexType>
            <xs:sequence>
                <xs:element  name="Category1">
                    <xs:complexType>
                        <xs:attribute name="MenuItem1" type="xs:string" use="required" />
                        <xs:attribute name="MenuItem2" type="xs:string" use="required" />
                        <xs:attribute name="MenuItem3" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

On the command line, I run the xsd file and it generated classes. MenuList and Object Factory.

Apptest.java

package com.xx.menu;

import java.io.File;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.UnmarshalException;
import javax.xml.bind.Unmarshaller;

public class TestNew {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try {
        JAXBContext jc = JAXBContext.newInstance("com.xx.menu");        //Create unmarshaller
        Unmarshaller um = jc.createUnmarshaller();

        File file = new File ("C:\\sample\\menulist.xml");

        JAXBElement element = (JAXBElement)um.unmarshal(file);

        Menulist menulist= (Menulist) element.getValue ();
        System.out.println("name : "+menulist.getMenu());

        }
     catch( UnmarshalException ue ) {
         ue.printStackTrace();
        System.out.println( "Caught UnmarshalException" );
    } catch( JAXBException je ) { 
        je.printStackTrace();
    } catch( Exception ioe ) {
        ioe.printStackTrace();
    }
    }

}

Error:

java.lang.ClassCastException: com.xx.menu.Menulist
    at com.xx.menu.TestNew.main(TestNew.java:26)

Could you help me where I am wrong ... I will be very grateful to you.

thank

+3
source share
2 answers

You feel too weird. You do not need to guess with JAXBElementif it Unmarshallerprovides you with an object Menulistdirectly. ClassCastExcepotionpretty much tells you what you need to do:

Menulist menulist= (Menulist) um.unmarshal(file);

JAXBElement , .

+3

, JAXBElement ?

JAXBElement JAXB, , , XML.

@XmlRootElement

Menulist Menulist. , , .

@XmlElementDecl

Menulist , , . @XmlRootElement ObjectFactory @XmlElementDecl. , JAXBElement .

, JAXBElement, JAXBIntrospector .

    JAXBIntrospector ji = jaxbContext.createJAXBIntrospector();
    Menulist menulist = (Menulist) ji.getValue(unmarshaller.unmarshal(xml));

+1

Source: https://habr.com/ru/post/1743925/


All Articles