Java tutorial
//package com.java2s; import java.io.FileInputStream; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; public class Main { public static synchronized Element getRootElement(String fileName) { if (fileName == null || fileName.length() == 0) { return null; } try { Element rootElement = null; FileInputStream fis = new FileInputStream(fileName); rootElement = getRootElement(fis); fis.close(); return rootElement; } catch (Exception e) { e.printStackTrace(); return null; } } public static synchronized Element getRootElement(InputStream is) { if (is == null) { return null; } Element rootElement = null; try { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = db.parse(is); rootElement = doc.getDocumentElement(); } catch (Exception e) { e.printStackTrace(); } return rootElement; } }