You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Michael Bishop <bi...@gmail.com> on 2012/12/11 22:39:43 UTC
MyDocument can't be cast to MyDocumentImpl?
Hello all,
I'm experiencing a weird issue in trying to load documents in XMLBeans.
I'm working with a NetBeans platform application and we use XMLBeans
extensively throughout the application. This issue is a "first" for me and
I'm not sure how to proceed.
I have an object that stores XML data as a String. So, elsewhere in my
application, I have something like this:
MyDocument doc = MyDocument.Factory.newInstance();
... // populate stuff here.
String myText = doc.xmlText();
Elsewhere in my application, I want to validate that the String I'm
receiving is indeed a valid instance of that document:
// Shortened for brevity's sake.
public boolean isValid(final String input) {
try {
MyDocument.Factory.Parse(input);
return true;
} catch (Exception ex) {
}
return false;
}
I get this perplexing error:
java.lang.ClassCastException: myPackage.impl.MyDocumentImpl cannot be cast
to myPackage.MyDocument
So, I put the whole thing in a unit test. I made a document, wrote it to a
String, then parsed it again. It works in a unit test. So there must be
something going wrong with the environment in my application.
Unfortunately, I don't know what that is, nor how to troubleshoot. Here's
what I know:
- The XMLBeans data is in a single module. There should be no duplicate
classes.
- I checked the ClassLoader of the MyDocument and MyDocumentImpl classes.
They're the same.
- I load/edit/save other XMLBeans documents throughout the application
without issue.
- The string data is stored as child text in another XML element. This may
be relevant:
String xmlText = myDocument.xmlText();
myOtherElement.setStringValue(xmlText);
...
String textToValidate = myOtherElement.getStringValue();
MyDocument.Factory.Parse(textToValidate);
Could this be mangling the structure? It looks fine when I log the value.
Anyway, further suggestions would be great.
Michael
Re: MyDocument can't be cast to MyDocumentImpl?
Posted by Michael Bishop <bi...@gmail.com>.
Hi Cezar,
I couldn't do it on the command line because it's a NetBeans module and
needs a NetBeans platform to run. I did the next best thing and wrote a
unit test around it. For whatever reason, you're correct. While writing
code in the test package, I noticed two instances of the same classes from
auto-completed import statements. In the actual source (not test) package,
only one instance of the class appeared. I don't know exactly how NetBeans
platform applications work with class loading in this regard. I could find
no reason that this class appeared in a unit test and not the "live"
application. I have a "legacy" suite in this NetBeans application where
older modules are stored. I have no idea why or how these classes are being
referenced, but they are. I removed the offending module from the legacy
suite and things worked fine.
Long story short, odd, NetBeans-related problem that took me some time to
track down. I still don't know the "why", but the problem is eliminated.
On Wed, Jan 30, 2013 at 1:26 PM, Cezar Andrei <ce...@oracle.com>wrote:
> Hi Michael,
>
> To me it seems you might have 2 different jars around for the same
> schema or some classloader issue. I would just make sure first it does
> run correctly from command line.
>
> Cezar
>
>
> On Tue, 2012-12-11 at 13:39 -0800, Michael Bishop wrote:
> > Hello all,
> >
> > I'm experiencing a weird issue in trying to load documents in
> > XMLBeans. I'm working with a NetBeans platform application and we use
> > XMLBeans extensively throughout the application. This issue is a
> > "first" for me and I'm not sure how to proceed.
> >
> > I have an object that stores XML data as a String. So, elsewhere in
> > my application, I have something like this:
> >
> > MyDocument doc = MyDocument.Factory.newInstance();
> > ... // populate stuff here.
> > String myText = doc.xmlText();
> >
> > Elsewhere in my application, I want to validate that the String I'm
> > receiving is indeed a valid instance of that document:
> >
> > // Shortened for brevity's sake.
> > public boolean isValid(final String input) {
> > try {
> > MyDocument.Factory.Parse(input);
> > return true;
> > } catch (Exception ex) {
> > }
> >
> > return false;
> > }
> >
> > I get this perplexing error:
> >
> > java.lang.ClassCastException: myPackage.impl.MyDocumentImpl cannot be
> > cast to myPackage.MyDocument
> >
> > So, I put the whole thing in a unit test. I made a document, wrote it
> > to a String, then parsed it again. It works in a unit test. So there
> > must be something going wrong with the environment in my application.
> > Unfortunately, I don't know what that is, nor how to troubleshoot.
> > Here's what I know:
> >
> > - The XMLBeans data is in a single module. There should be no
> > duplicate classes.
> > - I checked the ClassLoader of the MyDocument and MyDocumentImpl
> > classes. They're the same.
> > - I load/edit/save other XMLBeans documents throughout the application
> > without issue.
> > - The string data is stored as child text in another XML element.
> > This may be relevant:
> >
> > String xmlText = myDocument.xmlText();
> > myOtherElement.setStringValue(xmlText);
> > ...
> > String textToValidate = myOtherElement.getStringValue();
> > MyDocument.Factory.Parse(textToValidate);
> >
> > Could this be mangling the structure? It looks fine when I log the
> > value.
> >
> > Anyway, further suggestions would be great.
> >
> > Michael
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org
>
>
Re: MyDocument can't be cast to MyDocumentImpl?
Posted by Cezar Andrei <ce...@oracle.com>.
Hi Michael,
To me it seems you might have 2 different jars around for the same
schema or some classloader issue. I would just make sure first it does
run correctly from command line.
Cezar
On Tue, 2012-12-11 at 13:39 -0800, Michael Bishop wrote:
> Hello all,
>
> I'm experiencing a weird issue in trying to load documents in
> XMLBeans. I'm working with a NetBeans platform application and we use
> XMLBeans extensively throughout the application. This issue is a
> "first" for me and I'm not sure how to proceed.
>
> I have an object that stores XML data as a String. So, elsewhere in
> my application, I have something like this:
>
> MyDocument doc = MyDocument.Factory.newInstance();
> ... // populate stuff here.
> String myText = doc.xmlText();
>
> Elsewhere in my application, I want to validate that the String I'm
> receiving is indeed a valid instance of that document:
>
> // Shortened for brevity's sake.
> public boolean isValid(final String input) {
> try {
> MyDocument.Factory.Parse(input);
> return true;
> } catch (Exception ex) {
> }
>
> return false;
> }
>
> I get this perplexing error:
>
> java.lang.ClassCastException: myPackage.impl.MyDocumentImpl cannot be
> cast to myPackage.MyDocument
>
> So, I put the whole thing in a unit test. I made a document, wrote it
> to a String, then parsed it again. It works in a unit test. So there
> must be something going wrong with the environment in my application.
> Unfortunately, I don't know what that is, nor how to troubleshoot.
> Here's what I know:
>
> - The XMLBeans data is in a single module. There should be no
> duplicate classes.
> - I checked the ClassLoader of the MyDocument and MyDocumentImpl
> classes. They're the same.
> - I load/edit/save other XMLBeans documents throughout the application
> without issue.
> - The string data is stored as child text in another XML element.
> This may be relevant:
>
> String xmlText = myDocument.xmlText();
> myOtherElement.setStringValue(xmlText);
> ...
> String textToValidate = myOtherElement.getStringValue();
> MyDocument.Factory.Parse(textToValidate);
>
> Could this be mangling the structure? It looks fine when I log the
> value.
>
> Anyway, further suggestions would be great.
>
> Michael
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org