File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes cannot be cast to org.w3c.dom.Document 	a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark " cannot be cast to org.w3c.dom.Document 	a" Watch " cannot be cast to org.w3c.dom.Document 	a" New topic
Author cannot be cast to org.w3c.dom.Document a

Amit Hunashikatti

Joined: May 29, 2012
Posts: 2
I am trying to parse inputxml using parser.
Bellow is code
When I am calling parseXML(InputXML)
it is throwing error is follows

please suggest me how to resolve;-(

input xml is---><root><Value1>first</Value1><Value2>second</Value2></root>
parsing is completed
Error thrown in parser class
java.lang.ClassCastException: cannot be cast to org.w3c.dom.Document
at WLST.test1.parseXML(
at WLST.createINPUT.main(


package WLST;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.w3c.dom.*;

public class test1 {
String Res="Failed";
StringReader reader=null;
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();

public String parseXML(String InputXML)
DocumentBuilder db=dbf.newDocumentBuilder();
reader =new StringReader(InputXML);
System.out.println("input xml is--->"+InputXML.toString());
Document doc=(Document) db.parse(new InputSource(reader));
System.out.println("parsing is completed");
Element ele=(Element) ((Document) doc).getDocumentElement();
NodeList nl=((Document) ele).getElementsByTagName("root");
if(nl!=null && nl.getLength()>0)
for (int i=0;i<nl.getLength();i++)
Element el=(Element)nl.item(i);
NodeList val=((org.w3c.dom.Document) el).getElementsByTagName("value1");
if (val!=null && val.getLength()>0)
Element v=(Element) val.item(0);
String text=((Node) v).getFirstChild().getNodeValue();
System.out.println("value1 is--->"+text);

catch(Exception Ex)
System.out.println("Error thrown in parser class");
return Res;



John Jai

Joined: May 31, 2011
Posts: 1776
Hi Amit,

Welcome to the Ranch

1. The parse method of DocumentBuilder returns a Document instance. So you don't need to do a cast on it when you store it into a Document type variable. So the external cast here can be removed.

2. getDocumentElement() method in the Document class returns an Element. So again you don't need to cast it. Why do you cast the variable doc to type Document? It's already of the type Document.

3. The error should be thrown in this line of code where you try to cast an Element to a Document. It's an incompatible cast and hence you receive the ClassCastException at runtime. Why do you want to cast it anyways?

The basic thing you should learn is about "Java type casting". You should get lot of useful links if you search the internet.

And please UseCodeTags while you post code.
Amit Hunashikatti

Joined: May 29, 2012
Posts: 2
Hi jhon,
thanks for your response;-)
Actually I am using eclipse.
it was throwing error, and asking to map casting....
now its solved.
I agree. Here's the link:
subject: cannot be cast to org.w3c.dom.Document a
It's not a secret anymore!