tags:

views:

51

answers:

3

hi i have problems in converting an XML document type into a Document object.. this is the piece of code

Document doc=null;
doc = (Document) parser.parse(sourceFile);

for this line 2 it throws java classcast exception..

without the typecast it shows error as

Type mismatch: cannot convert from org.w3c.dom.Document to javax.swing.text.Document

how do i now typecast properly? any suggestions??

+4  A: 

The problem is that there's a collision in the unqualified names.

That is, as a result of your import statements, the unqualified name Document refers to javax.swing.text.Document, but you really need org.w3c.dom.Document instead (that's the type that the parser returns).

You can fix this by using the fully qualified name:

org.w3c.dom.Document doc = (org.w3c.dom.Document) parser.parse(sourceFile);

Or, you can also specifically import the particular Document as follows:

import javax.swing.text.*;
import org.w3c.dom.*;
import org.w3c.dom.Document;

//...

Document doc = (Document) parser.parse(sourceFile);

This is called the single-type import declaration (JLS 7.5.1), and it can be used to "shadow" other declarations.

polygenelubricants
Indeed. Check your imports :)
extraneon
hi it still shows the same exception :(
shil
@shil: can you qualify your own statement "hi i have problems in converting an XML document type into a `Document` object." -- which `Document` are you talking about here? Because if it's `javax.swing.text.Document` then unfortunately no, you can't cast it. You'd have to do a more elaborate conversion enabled by libraries (not by the Java language). See Michael's answer.
polygenelubricants
yeah its javax.swing.text.Document.. and i couldn figure out what i should do, from micheal's answer.. could you elaborate?
shil
@shil: What would you like to do with the `swing.Document`? Can you elaborate further? Maybe edit the question with your intent.
polygenelubricants
+2  A: 

Those two interfaces have the same name, but are totally unrelated. You cannot cast between them - it doesn't make sense as they represent quite different concepts (OK, theoretically, you could have a Swing component that displays XML trees and uses a DOM Document as its model, but I don't think that's what you have).

What you probably want to do is to take the unparsed XML and call setText(xmlText) on the swing component you want to display it with.

Michael Borgwardt
+1, turns out that's what OP intended to do after all. You may want to take over from here.
polygenelubricants
A: 

Is your application a Swing application or not?

You are probably using an IDE which you automatically let organize the imports in your source file. The IDE added an import for javax.swing.text.Document instead of org.w3c.dom.Document. This is something I've encountered myself often while using the Eclipse IDE.

What you have to do: Remove this line from the top of your source code file:

import javax.swing.text.Document;

Replace it with:

import org.w3c.dom.Document;
Jesper
Jesper: the `import` turns out to NOT be the problem: OP actually wants to convert a w3c `Document` to a Swing `Document` somehow.
polygenelubricants
Ok. Well, that's ofcourse not easy, because `org.w3c.dom.Document` and `javax.swing.text.Document` are two totally different things, even though they are both called `Document`.
Jesper