Java tutorial
//package com.java2s; import java.io.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.*; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class Main { public static Document readFileAsDoc(File file) throws SAXException, IOException, Exception { return readFileAsDoc(readFileAsStr(file)); } public static Document readFileAsDoc(String xml) throws SAXException, IOException, Exception { org.w3c.dom.Document doc = null; StringReader sr = new StringReader(xml); DocumentBuilder bldr = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = bldr.parse(new InputSource(sr)); return doc; } public static String readFileAsStr(File file) throws IOException { StringBuffer sb = new StringBuffer(); if (file == null) return ""; if (!file.exists()) return ""; LineNumberReader lnr = null; try { lnr = new LineNumberReader(new FileReader(file)); String s = lnr.readLine(); while (s != null) { sb.append(s); s = lnr.readLine(); } } catch (java.io.IOException ioe) { throw ioe; } finally { if (lnr != null) { lnr.close(); } } return sb.toString(); } }