You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Radu Preotiuc-Pietro <ra...@bea.com> on 2005/05/25 22:24:51 UTC

RE: ClassCastException

Looks like you've hit the runtime equivalent of the "multiple occurences" error.
Basically, the reason XmlBeans can just read a document and validate it is that it uses the classpath to find a definition for the root element in a document. So what is happening is that, since you have multiple definitions for the same element, first one found on the classpath wins out, but this may not be the one that you are expecting. And by the way, since they all have the same name, how do you know which one you're expecting?
 
There are several ways to fix this:
- the obvious one, use namespaces, this is exactly what namespaces are for!
- use XmlOptions.setDocumentType(x.y.z.RETURNDATADocument.type) before parsing the document: this works as long as you are not using "xsi:type" in your documents
- use XmlOptions.setLoadReplaceDocumentElement() so that you can use namespaces in your Schemas, but not in the instance documents; same caveat as previous point
- use custom typeloaders via XmlBeans.typeLoaderForResource(...).parse(); this allows you to take control over what type library is used when loading/validating document so it works in 100% of cases, but of course, involves writing more code.
 
Radu

-----Original Message-----
From: moraru_etc@spymac.com [mailto:moraru_etc@spymac.com]
Sent: Wednesday, May 25, 2005 7:26 AM
To: user@xmlbeans.apache.org
Subject: RE: ClassCastException



I discovered that the problem is not the scema or the xml, but the classpath. 

I have many schemas and none of them have namespace declared. I can't change this because the schemas are coming form a service provider.

Approx. all the tags in schemas have the same name. You can find <RETURNDATA> and <DATA> in each schema. The first time i tried to generate the jar i got the "multiple occurences" error.

So i wrote a .xsdconfig for each schema, thus every schema have it's own package like: a.b.c.RETURNDATADocument or x.y.z.RETURNDATADocument 

After the classCastException i added for each .xsdconfig file a _$ suffix where $ in (1, ...20).

So now all the classes are a.b.c.RETURNDATADocument_1, ...etc

If i try to parse a a.b.c.RETURNDATADocument_1 first , and it's jar in the first one in CLASSPATH it will work, but if i try after this to parse a x.y.z.RETURNDATADocument_2 i receive ClassCAstException.

If i put the jars in another order, the first operation will give ClassCast

 

PLease give me a clue at least!!



On Wed May 25 4:52 , 'Don Stewart' <Do...@corizon.com> sent:



Can you post the whole schema as I cannot see from the schema supplied the whole problem.
 
Thanks
 
Don

  _____  

From: moraru_etc@spymac.com [mailto:moraru_etc@spymac.com] 
Sent: 25 May 2005 10:37
To: user@xmlbeans.apache.org
Subject: Re: ClassCastException 


I made a small test: i used xmlbeans to generate a sample xml file then i tried to parse it. I received ClassCastException again!!! Please help me!!



On Wed May 25 2:38 , <mo...@spymac.com> sent:



Hi!

I'm receiving a ClassCastException when i'm trying to parse a VALID xml file.I'm sure the xmlbeans.jar is the right one and the schema and xml file are OK. PLease give me a clue. I pasted here a piece form xml file and xml schema

Code:

fr.xxx.yyy.zzz.RETURNDATADocument rd = fr.xxx.yyy.zzz.RETURNDATADocument.Factory
     .parse(new File("xxxx.xml"));

Schema:

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

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:annotation>

<xsd:documentation xml:lang="en">

Results of Hotel Details request, for a given Hotel Code. 

Any error messages in "MESSAGE" element with

text "Error" followed by the error description

</xsd:documentation>

</xsd:annotation>

<xsd:element name="RETURNDATA">

<xsd:complexType>

 

-----------XML:

<?xml version='1.0' encoding='UTF-8'?>

<RETURNDATA type='HDR' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='http://xx.yy.zz/aaa/HotelDetailRequestV4Rcv.xsd'>

<MESSAGE>Details of the requested Hotel</MESSAGE>

<DATA HOTEL_CODE='AUS' COUNTRY_CODE='AUS' CITY_CODE='SZG' PRINCIPAL_CODE='TRAVCO' BOARD_BASIS_CODE='BB' CATEGORY_CODE='HOT'>

<HOTEL_NAME>Austrotel</HOTEL_NAME>

<COUNTRY_NAME>AUS</COUNTRY_NAME>

............

</DATA>

</RETURNDATA>


--------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org For additional commands, e-mail: user-help@xmlbeans.apache.org 


--------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org For additional commands, e-mail: user-help@xmlbeans.apache.org 


--------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org For additional commands, e-mail: user-help@xmlbeans.apache.org