1

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??

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@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.
yeah its javax.swing.text.Document.. and i couldn figure out what i should do, from micheal's answer.. could you elaborate?
@shil: What would you like to do with the swing.Document? Can you elaborate further? Maybe edit the question with your intent.
2

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.

1 Comment

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

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;

2 Comments

Jesper: the import turns out to NOT be the problem: OP actually wants to convert a w3c Document to a Swing Document somehow.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.