Java tutorial
//package com.java2s; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; public class Main { public static DocumentBuilderFactory getDocumentBuilderFactory() { return getDocumentBuilderFactory(true, true); } public static DocumentBuilderFactory getDocumentBuilderFactory(final boolean namespaceAware) { return getDocumentBuilderFactory(namespaceAware, true); } public static DocumentBuilderFactory getDocumentBuilderFactory(final boolean namespaceAware, boolean disableDtdXxe) { final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); if (namespaceAware) { documentBuilderFactory.setNamespaceAware(namespaceAware); } if (disableDtdXxe) { setDocBuilderFeature(documentBuilderFactory, "http://xerces.apache.org/xerces-j/features.html#external-general-entities", false); setDocBuilderFeature(documentBuilderFactory, "http://xerces.apache.org/xerces2-j/features.html#external-general-entities", false); setDocBuilderFeature(documentBuilderFactory, "http://xml.org/sax/features/external-general-entities", false); setDocBuilderFeature(documentBuilderFactory, "http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl", true); setDocBuilderFeature(documentBuilderFactory, "http://apache.org/xml/features/disallow-doctype-decl", true); } return documentBuilderFactory; } /** * Set a {@link javax.xml.parsers.DocumentBuilderFactory} feature, suppressing any exceptions * * @param documentBuilderFactory Document builder factory * @param name Feature name * @param value Feature value */ private static void setDocBuilderFeature(DocumentBuilderFactory documentBuilderFactory, String name, boolean value) { try { documentBuilderFactory.setFeature(name, value); } catch (ParserConfigurationException e) { } } }