Here you can find the source of getRoot(Document doc)
public static final Element getRoot(Document doc)
//package com.java2s; //License from project: Open Source License import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { public static final Element getRoot(Document doc) { Node n = doc.getFirstChild(); while (n != null) { if (n instanceof Element) { return (Element) n; }// w ww . j a v a 2 s .co m n = n.getNextSibling(); } return null; } }