List of usage examples for javax.xml.parsers DocumentBuilderFactory setFeature
public abstract void setFeature(String name, boolean value) throws ParserConfigurationException;
From source file:Main.java
/** * Creates a new {@link DocumentBuilder} object. *///from w w w . jav a2s . c o m private static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true); factory.setNamespaceAware(true); factory.setValidating(false); factory.setIgnoringComments(true); factory.setExpandEntityReferences(false); factory.setCoalescing(false); factory.setFeature("http://xml.org/sax/features/external-general-entities", false); factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); return factory.newDocumentBuilder(); }
From source file:Main.java
public static Document parseDoc(File xmlFile) throws ParserConfigurationException, SAXException, IOException { try (InputStream is = new FileInputStream(xmlFile)) { BufferedInputStream in = new BufferedInputStream(is); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setFeature("http://xml.org/sax/features/external-general-entities", false); factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource source = new InputSource(in); return builder.parse(source); }/* w w w .j a v a 2 s . c om*/ }
From source file:Main.java
/** * Set a {@link javax.xml.parsers.DocumentBuilderFactory} feature, suppressing any exceptions * * @param documentBuilderFactory Document builder factory * @param name Feature name * @param value Feature value *///from w ww . ja v a 2s. c om private static void setDocBuilderFeature(DocumentBuilderFactory documentBuilderFactory, String name, boolean value) { try { documentBuilderFactory.setFeature(name, value); } catch (ParserConfigurationException e) { } }
From source file:Main.java
/** * Create a new DocumentBuilder which processes XML securely. * * @return a DocumentBuilder//from w ww . j av a 2s . c o m */ public static DocumentBuilder createDocumentBuilder() { try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); return documentBuilderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new IllegalStateException(e); } }
From source file:Main.java
public static Document parseDoc(final InputStream is) throws ParserConfigurationException, SAXException, IOException { try {/*ww w . j a va2 s . com*/ BufferedInputStream in = new BufferedInputStream(is); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setFeature("http://xml.org/sax/features/external-general-entities", false); factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource source = new InputSource(in); return builder.parse(source); } finally { is.close(); } }
From source file:Main.java
public static Document createNewDocument() throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);//from w w w . j a v a2 s . c om dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); //dbf.setIgnoringElementContentWhitespace(true); return dbf.newDocumentBuilder().newDocument(); }
From source file:Main.java
static DocumentBuilderFactory getSecureDocumentBuilderFactory() throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);/*from w w w. j a v a2 s .co m*/ factory.setFeature("http://xml.org/sax/features/external-general-entities", false); factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); return factory; }
From source file:Main.java
/** * This will parse an XML stream and create a DOM document. * * @param is The stream to get the XML from. * @return The DOM document./*w ww . jav a2 s .c om*/ * @throws IOException It there is an error creating the dom. */ public static Document parse(InputStream is) throws IOException { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); builderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false); builderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); builderFactory.setXIncludeAware(false); builderFactory.setExpandEntityReferences(false); DocumentBuilder builder = builderFactory.newDocumentBuilder(); return builder.parse(is); } catch (FactoryConfigurationError e) { throw new IOException(e.getMessage(), e); } catch (ParserConfigurationException e) { throw new IOException(e.getMessage(), e); } catch (SAXException e) { throw new IOException(e.getMessage(), e); } }
From source file:Main.java
public static Document getXmlDocFromString(String xml) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);// w ww .j av a 2 s. c o m dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); DocumentBuilder builder = dbf.newDocumentBuilder(); builder.setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(new StringReader("")); } }); return builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8"))); }
From source file:Main.java
public static Document getXmlDocFromURI(InputStream is) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);//from w ww . j a v a 2 s . c o m dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); DocumentBuilder builder = dbf.newDocumentBuilder(); builder.setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(new StringReader("")); } }); return builder.parse(is); }