• Post Reply Bookmark Topic Watch Topic
  • New Topic

Node to Element conversion

 
Kurt Peters
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some simple code that I am getting a weird error about. Eclipse is not having a problem with the recast, but at runtime I get this error:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to org.w3c.dom.Element
at org.mitre.e500.DOMParser.fillInternalChannels(DOMParser.java:180)
at org.mitre.e500.DOMParser.parseDocument(DOMParser.java:162)
<snip>



Earlier, I didn't have a problem casting from a Node to an Element:


The weird thing is that this works:

Any thoughts?
Kurt
 
Mikemil Miller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting the same thing both inside and outside of Eclipse. I am running RESTEasy and simulating their Simple example. Any luck on this.
 
Kurt Peters
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I ended up get using the "get first element" construct and using the nodelist one, since I couldn't figure out what was happening.
Kurt
 
Mikemil Miller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally found a hit late last night when searching for "DeferredTextImpl ClassCastException Element Node" and the answer in the list thread
http://coding.derkeiler.com/Archive/Java/comp.lang.java.help/2007-06/msg00494.html

Apparently getting the item doesn't guarantee that it is an 'Element' - the "#text' item was found. I changed my offending code to check the nodeType and sure enough, there was a '#text' node and after that was the Element.

Hope this helps!
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"get first element" construct


AHA! you should never never never depend on that firstElement being what you expect.

The reason being that if XML has been formatted for readability, there is a Text type Node between Elements. Humans tend ot overlook this but XML parsers never do.

Bill
 
Kurt Peters
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I don't understand descendency as well as I thought I did. I thought an Element was a descendent of a node?

public interface Element extends Node
 
Mikemil Miller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So whatever the 'type' is of that text-type - it apparently does not implement Element.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, Text extends CharacterData which extends Node.

Regards,
Dan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!