private static javax . xml . parsers . DocumentBuilder getDocumentBuilder ( Schema schema , boolean useNamespace ) throws ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory . newInstance ( ) ; dbf . setIgnoringComments ( true ) ; dbf . setIgnoringElementContentWhitespace ( true ) ; dbf . setCoalescing ( true ) ; if ( schema != null ) { // because schema is used throws fatal error if XML document contains DOCTYPE declaration dbf . setFeature ( FEAT_DOCTYPE_DECL , true ) ; // excerpt from document builder factory api: // Note that "the validation" here means a validating parser as defined in the XML recommendation. In other words, // it essentially just controls the DTD validation. // To use modern schema languages such as W3C XML Schema or RELAX NG instead of DTD, you can configure your parser // to be a non-validating parser by leaving the setValidating(boolean) method false, then use the setSchema(Schema) // method to associate a schema to a parser. dbf . setValidating ( false ) ; // XML schema validation requires namespace support dbf . setFeature ( FEAT_SCHEMA_VALIDATION , true ) ; dbf . setNamespaceAware ( true ) ; dbf . setSchema ( schema ) ; } else { // disable parser XML schema support; it is enabled by default dbf . setFeature ( FEAT_SCHEMA_VALIDATION , false ) ; dbf . setValidating ( false ) ; dbf . setNamespaceAware ( useNamespace ) ; } javax . xml . parsers . DocumentBuilder db = dbf . newDocumentBuilder ( ) ; db . setEntityResolver ( new EntityResolverImpl ( ) ) ; db . setErrorHandler ( new ErrorHandlerImpl ( ) ) ; return db ; }
Get XML document builder .