Java tutorial
//package com.java2s; //License from project: Apache License import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; public class Main { private static DocumentBuilderFactory mFactory = null; /** * @param is the input stream to read * @return the document contained in that input stream */ public static Document readStream(InputStream is) { ensureFactory(); try { DocumentBuilder builder = mFactory.newDocumentBuilder(); return builder.parse(is); } catch (Exception e) { e.printStackTrace(); return null; } } private static synchronized void ensureFactory() { if (mFactory == null) { mFactory = DocumentBuilderFactory.newInstance(); mFactory.setValidating(false); mFactory.setCoalescing(true); mFactory.setExpandEntityReferences(false); } } }