Here you can find the source of parse(File file)
public static Document parse(File file)
//package com.java2s; //License from project: Open Source License import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.io.StringReader; import java.lang.ref.SoftReference; public class Main { private static SoftReference<DocumentBuilderFactory> DOCUMENT_BUILDER_FACTORY = new SoftReference<>(null); public static Document parse(File file) { if (file == null) throw new IllegalArgumentException("Must provide non-null XML input to parse!"); try {/*from www . ja v a2 s.c om*/ final Document document; DocumentBuilder documentBuilder = createDocumentBuilder(); document = documentBuilder.parse(file); return document; } catch (SAXException e) { throw new RuntimeException("Error parsing xml: " + e.getMessage(), e); } catch (IOException e) { throw new RuntimeException("Error parsing xml: " + e.getMessage(), e); } } public static Document parse(String xml) { if (xml == null) throw new IllegalArgumentException("Must provide non-null XML input to parse!"); return parse(new StringReader(xml)); } public static Document parse(InputStream xml) { if (xml == null) throw new IllegalArgumentException("Must provide non-null XML input to parse!"); return parse(new InputSource(xml)); } public static Document parse(Reader xml) { if (xml == null) throw new IllegalArgumentException("Must provide non-null XML input to parse!"); return parse(new InputSource(xml)); } public static Document parse(InputSource src) { if (src == null) throw new IllegalArgumentException("Must provide non-null XML input to parse!"); DocumentBuilder documentBuilder = createDocumentBuilder(); try { return documentBuilder.parse(src); } catch (SAXException e) { throw new RuntimeException("Error parsing xml: " + e.getMessage(), e); } catch (IOException e) { throw new RuntimeException("Error parsing xml: " + e.getMessage(), e); } } /** * Create a new (namespace-aware) DocumentBuilder * * @return */ public static DocumentBuilder createDocumentBuilder() { try { DocumentBuilderFactory factory = DOCUMENT_BUILDER_FACTORY.get(); if (factory == null) { factory = createDocumentBuilderFactory(); DOCUMENT_BUILDER_FACTORY = new SoftReference<>(factory); } return factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new RuntimeException("Error creating DocumentBuilder: " + e.getMessage(), e); } } private static DocumentBuilderFactory createDocumentBuilderFactory() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); return factory; } }