• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

com.sun.org.apache.xerces.internal.dom.DeferredElementImpl cannot be cast to org.w3c.dom.Document a

 
Amit Hunashikatti
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
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: com.sun.org.apache.xerces.internal.dom.DeferredElementImpl cannot be cast to org.w3c.dom.Document
at WLST.test1.parseXML(test1.java:33)
at WLST.createINPUT.main(createINPUT.java:20)





-----------------------------------------------------------------------------------------------------------------------------------------------------------------


package WLST;

import java.io.StringReader;

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)
{
try
{
dbf.setValidating(false);
dbf.setNamespaceAware(false);
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");
Ex.printStackTrace();
}
return Res;

}

}


 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jhon,
thanks for your response;-)
Actually I am using eclipse.
it was throwing error, and asking to map casting....
now its solved.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic