I am doing a lot of XML parsing/transformation using the org.w3c.dom classes.
A Document object provides a lot of handy methods for traversing a document
that are not available to a Node object (e.g. getElementsByTagName(String)). Quite often I find myself getting Node elements and wanting to use the
convenience methods of Document on the child Node instances (one node at a time).
Is there any way I can convert a Node to a Document?
The
Document.adoptNode(Node)
or
Document.importNode(Node,boolean)
seems to provide what I want, but I cannot figure how to get an
instance of a blank Document in which to embed the node. Is there a way to get
a blank Document?
Or am I thinking about/approaching this in an entirely wrong way?
Invitatons to RTFM (pointing to a specific method/class that can clear my
conundrum) would be welcome, as well as any other thoughts or strategies.
getElementsByTagName()returns aNodeList, but every item of the node list is an instance ofElement. AndElementalso has agetElementsByTagName()method.Just cast the nodes from the node list to
Element.