Java: Getting the Document from a Node or Element

When we for instance wishes to add a new element we need to pass along the document. If we have a element or a node we can get the document that way. Simply call getOwnerDocument and we are good.

Element element = //code to get a element
Node node = //code to get a node
 
//document from Node
Document document = node.getOwnerDocument();
 
//document from a Element
Document document = element.getOwnerDocument();