Here you can find the source of parseFile(Class> context, String name)
public static Document parseFile(Class<?> context, String name) throws IOException
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; public class Main { /** Parse an XHTML file from our CLASSPATH and return the instance. */ public static Document parseFile(Class<?> context, String name) throws IOException { try (InputStream in = context.getResourceAsStream(name)) { if (in == null) { return null; }/*from w w w . j a va 2s.c om*/ Document doc = newBuilder().parse(in); compact(doc); return doc; } catch (SAXException | ParserConfigurationException | IOException e) { throw new IOException("Error reading " + name, e); } } /** Parse an XHTML file from the local drive and return the instance. */ public static Document parseFile(Path path) throws IOException { try (InputStream in = Files.newInputStream(path)) { Document doc = newBuilder().parse(in); compact(doc); return doc; } catch (NoSuchFileException e) { return null; } catch (SAXException | ParserConfigurationException | IOException e) { throw new IOException("Error reading " + path, e); } } private static DocumentBuilder newBuilder() throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setExpandEntityReferences(false); factory.setIgnoringComments(true); factory.setCoalescing(true); return factory.newDocumentBuilder(); } private static void compact(Document doc) { try { String expr = "//text()[normalize-space(.) = '']"; XPathFactory xp = XPathFactory.newInstance(); XPathExpression e = xp.newXPath().compile(expr); NodeList empty = (NodeList) e.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < empty.getLength(); i++) { Node node = empty.item(i); node.getParentNode().removeChild(node); } } catch (XPathExpressionException e) { // Don't do the whitespace removal. } } }