javax.xml.bind.JAXBElement unmarshal error

By W.ZH Apr 2011

Issue:

When I use JAXB 2.0 to unmarshal an XML to a java Object, I get an error that :

java.lang.ClassCastException: javax.xml.bind.JAXBElement cannot be cast to com.oracle.daps.Path.

the code is sth like this :

JAXBContext jc = JAXBContext.newInstance(“com.oracle.daps.obj”);

Unmarshaller u = jc.createUnmarshaller();

MyJavaObj jo = (MyJavaObj) u.unmarshal(new StreamSource(new StringReader(“myxml.xml”)));

Reason:

In infct when i run this kind of code when using jaxb 1.0 , i do not get any error, but later when i update my XML xsd schema and try to generate a set of new JAVA obj by new xsd, I use the JAXB 2 to do that, so my unmarshal get this error, reason is that JAXBElement  in fact contain the obj in its value, i need a layer more to convert this XML to Java Obj.

Solution:

JAXBContext jc = JAXBContext.newInstance(“com.oracle.daps.obj”);

Unmarshaller u = jc.createUnmarshaller();

MyJavaObj jo = (MyJavaObj) ((
JAXBElement) u.unmarshal(new StreamSource(new StringReader(“myxml.xml”)))).getValue()

Advertisements

XML Menu Model

XML Menu Model

The XML menu model represents navigation for a page hierarchy in XML format. In the XML menu model metadata, a page hierarchy is described within the menu element, which is the root element of the file. Every XML menu model metadata file is required to have a menu element. Only one menu element is allowed.

A hierarchy is comprised of nodes. Each node corresponds to a page. The menu element can contain elements for the following nodes:

■ groupNode:

Contains one or more child nodes to navigate to indirectly. It must reference the id of at least one of its child nodes, which can be either another group node or an item node.

■ itemNode:

Specifies a node that performs navigation upon user selection.

■  sharedNode:

References another XML menu. A sharedNode is not a true node; it does not perform navigation nor does it render anything on its own. It is simply an include mechanism. Use a sharedNode to link or include various menus into a larger hierarchy.