简体   繁体   中英

cast to SVGSVGElement from org.w3c.dom.Element fails

Seriously, if I cast like this:

(SVGSVGElement) (((SVGImage)blabla).getDocument().getDocumentElement())

it works, but if I try

(SVGSVGElement)doc.getElementById(some_id)

I get cast error, but both getDocumentElement() and getElementById() returns the same type org.w3c.dom.Element , my global goal is to get the X-coordinates of some image on my svg-scene loaded from file, in order to zoom scene to make this image fit in visible area. I tried to achieve that with ((SVGSVGElement)doc.getElementById(some_id)).getBBox().getX() but you see what I got.

org.w3c.dom.Element is the one of the lowest intefaces in DOM architecture, and lots of class extend this, particular SVGSVGElement .

You need to figure out, what type is returned in doc.getElementById(some_id) and then do the casting according that information:

log.debug("Returned class is - " + doc.getElementById(some_id).getClass().getName();
//or
System.out.println("Returned class is - " + doc.getElementById(some_id).getClass().getName();

And the most easiest way is to try to know the type directly in the debugger.

And the only cases, which I know, that type seems to be right, but ClassCastException occurs:

  • classloaders issues (when one class from 2 different libraries was loaded into 2 different classloaders)
  • AOP proxy issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM