Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassCastException with Document, DocumentTraversal

 
Jim Henessey
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am getting a ClassCastException with the code listed below.
The problem is with the line: "DocumentTraversal traversal=(DocumentTraversal) doc", where I get a java.lang.ClassCastException error.

A similar problem was brought up at Previous JavaRanch Topic - what am I doing wrong?and answered by someone with considerable authourity - however, as far as
I can tell Document to DocumentTraversal casting is permitted:

XML APIs Quick Reference
(under the DocumentTraversal section)

Also, most of this code (including the casting) is from the relatively authouritative ibm site:
Using a DOM NodeFilter

Code follows below:



Edit Comment: Added [ code ] [ / code ] tags, without the space.
[ November 02, 2004: Message edited by: Madhav Lakkapragada ]
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
psssstttt......you left out a key peice of info in that link that you
posted. From the DocumentTraversal section of the XML API link that you
posted -


In implementations that support the traversal API (which can be
determined by invoking the hasFeature("Traversal", "2.0" ) method in the
Document or DOMImplementation classes)
all objects that implement
Document also implement DocumentTraversal. That is, to create a
DocumentTraversal object, just cast a Document to DocumentTraversal.


So, I would change the code and invoke the hasFrature("Traversal", "2.0")
API and then see what type of results you get.

Please do let us know what you find.
Thanks.

- m
 
Jim Henessey
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Madhav Lakkapragada:
psssstttt......you left out a key peice of info in that link that you
posted. From the DocumentTraversal section of the XML API link that you
posted -



So, I would change the code and invoke the hasFrature("Traversal", "2.0")
API and then see what type of results you get.

Please do let us know what you find.
Thanks.

- m



Oh wow - thanks for catching that. That is the source of the problem - when I do a test, I get back that the feature doesn't exist. I'll look for options around this.

Thanks again for your help.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a hunch. Glad I could help.
Do keep posting.

- m
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic