DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); String FEATURE = null; try { // This is the PRIMARY defense. If DTDs (doctypes) are disallowed, almost all // XML entity attacks are prevented FEATURE = "http://apache.org/xml/features/disallow-doctype-decl"; dbf.setFeature(FEATURE, true);
// If you can't completely disable DTDs, then at least do the following FEATURE = "http://xml.org/sax/features/external-general-entities"; dbf.setFeature(FEATURE, false);