You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Peter Meyer <pe...@hotmail.com> on 2001/02/21 20:10:02 UTC

How to traverse a DOM in an object-oriented manner? Without switch{}?

Hi,
I have been playing around a bit with the DOM samples in the xerces 
distribution.
In the DOMCount example, to mention one instance, there is code of the type:
int type = node.getNodeType();
        switch (type) {
        // print document
        case Node.DOCUMENT_NODE: {
        ...
        }

        // print element with attributes
        case Node.ELEMENT_NODE: {
        ...
        }

        ...
}

Of course, that works, but this is not really very nice code in a real-world 
application. Ideally, I would like to use a visitor pattern to use 
polymorphism to choose the action to be taken based on the nodes - but I 
can't see a way to do this.

Why? Because the element hierarchy is based on the Node interface of the DOM 
which does not provide the hooks to do such things. And although I can 
subclass the implementation classes used in the document, that does not 
really help, as they still all look like "Node" to the DOM.

Is there any way to do something more robust and maintainable, like for 
example a visitor, to parse a DOM and execute node-type specific 
functionality without reverting to a switch statement?

Thanks for any suggestion...

    PM
_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.


Re: How to traverse a DOM in an object-oriented manner? Without switch{}?

Posted by Arnaud Le Hors <le...@us.ibm.com>.
There is currently no support for the visitor pattern in the DOM.
See http://www.w3.org/DOM/faq.html#visitor
-- 
Arnaud  Le Hors - IBM Cupertino, XML Strategy Group