How to put random objects onto a stack without having the objects turn into DeferredElementImpl?

StackOverflow https://stackoverflow.com/questions/13312452

  •  28-11-2021
  •  | 
  •  

Question

I'm trying to put both Strings and Nodes onto a Stack. I use the NodeList method item(int) to obtain a Node from the NodeList. Then, I push this Node to the Stack. My only problem is that I doesn't see it as a Node unless I have something wrong.

When I call getClass() method on the Node and it returns this:

class com.sun.org.apache.xerces.internal.dom.DeferredElementImpl

When I call that same method on NodeList (getClass()), it returns this:

class com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl

Both times when I call the getClass() method, it is before I put it on the Stack. I want to be able to any different Objects onto a Stack, and be able to filter through all the objects on the Stack to figure out which class they belong to. Thanks!

Was it helpful?

Solution

Node and NodeList are interfaces not class.

instanceof was made for you.

This code :

DocumentBuilderFactory dbf   = DocumentBuilderFactory.newInstance();
DocumentBuilder        db    = dbf.newDocumentBuilder();
Document               doc   = db.parse( new File( "build.xml" ));
NodeList               nodes = doc.getElementsByTagName( "target" );
System.err.println( nodes instanceof NodeList );
System.err.println( nodes.item( 0 ) instanceof Node );
System.err.println( nodes.item( 0 ) instanceof Element );

outputs:

true
true
true

You may write, after stack.pop(), if( var instance of String ) or if( var instanceof Node )... but IMHO it's not a good idea to mix different type into a single container.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top