Java tutorial
//package com.java2s; import com.sun.org.apache.xerces.internal.parsers.DOMParser; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; import java.io.FilterInputStream; import java.io.InputStream; public class Main { public static Document createDocument(InputStream input) { DOMParser parser = null; try { parser = getLargeParser(input.available()); parser.parse(new InputSource(new FilterInputStream(input) { public void close() { // disable close. Because createDocument calls close, but it shoudn't do it. } })); return parser.getDocument(); } catch (Exception e) { throw new RuntimeException(e); } } private static DOMParser getLargeParser(int size) { DOMParser parser = new DOMParser(); try { parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true); parser.setProperty("http://apache.org/xml/properties/input-buffer-size", size); } catch (SAXNotRecognizedException e) { throw new IllegalArgumentException(); } catch (SAXNotSupportedException e) { throw new IllegalArgumentException(); } return parser; } }