Accessing the Root Element Node of a Document

If you have a reference to the Document, you can loop through all immediate child nodes and look for a Element Node.


Document doc;
...
NodeList children = doc.getChildNodes();
for (int j=0; j<children.getLength(); j++) {
  Node n = children.item(j);
  if (n.getNodeType() == Node.ELEMENT_NODE) {
    System.out.println("Root Node name is: " + n.getNodeName());
  }
}
  

Published: Monday, 16 August 2004