List of usage examples for javax.xml.parsers DocumentBuilderFactory setValidating
public void setValidating(boolean validating)
From source file:Main.java
private static void validateDTD0(String xmlFile, final String dtdPath) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setValidating(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); if (dtdPath != null && !dtdPath.isEmpty()) { documentBuilder.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(new FileInputStream(dtdPath)); }// w w w . j a v a 2 s .c o m }); } documentBuilder.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException exception) throws SAXException { } public void fatalError(SAXParseException exception) throws SAXException { throw exception; } public void error(SAXParseException exception) throws SAXException { throw exception; } }); documentBuilder.parse(new FileInputStream(xmlFile)); }
From source file:Main.java
public static Document load(File fileName) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(false);//from w ww. java 2 s . co m factory.setIgnoringElementContentWhitespace(true); factory.setValidating(false); factory.setCoalescing(false); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(fileName); }
From source file:com.mingo.parser.xml.dom.DocumentBuilderFactoryCreator.java
/** * Creates DocumentBuilderFactory.//from w w w.jav a 2 s. c o m * * @param parserConfiguration {@link ParserConfiguration} * @return DocumentBuilderFactory a factory API that enables applications to obtain a * parser that produces DOM object trees from XML documents * @throws ParserConfigurationException {@link ParserConfigurationException} */ public static DocumentBuilderFactory createDocumentBuilderFactory(ParserConfiguration parserConfiguration) throws ParserConfigurationException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setValidating(parserConfiguration.isValidate()); documentBuilderFactory.setNamespaceAware(parserConfiguration.isNamespaceAware()); documentBuilderFactory.setFeature(DYNAMIC_VALIDATION, true); List<Source> sourceList = createSchemaSources(parserConfiguration.getXsdSchemaPaths()); if (CollectionUtils.isNotEmpty(sourceList)) { documentBuilderFactory.setSchema(createSchema(sourceList)); } return documentBuilderFactory; }
From source file:Main.java
public static Document createDocument() { try {/*from w ww .j av a 2 s. c o m*/ // Use JAXP to create a document builder DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setExpandEntityReferences(false); builderFactory.setValidating(false); builderFactory.setNamespaceAware(true); builderFactory.setIgnoringComments(true); builderFactory.setCoalescing(true); builderFactory.setIgnoringElementContentWhitespace(true); return builderFactory.newDocumentBuilder().newDocument(); } catch (ParserConfigurationException errParser) { throw new RuntimeException("Error getting XML parser", errParser); } }
From source file:io.leishvl.core.xml.XmlHelper.java
public static String prettyPrint(final String payload) { checkArgument(isNoneBlank(payload), "Uninitialized XML payload"); String docStr;// w w w . j ava 2 s . c o m try { final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); final DocumentBuilder db = dbf.newDocumentBuilder(); final Document document = db.parse(new ByteArrayInputStream(payload.getBytes())); docStr = prettyPrint(document); } catch (Exception e) { docStr = null; LOGGER.warn("Failed to convert DOM document to String", e); } return docStr; }
From source file:Main.java
/** * Uses the current DocumentBuilderFactory to converts a String * representation of XML into a Document. * * @param xml XML serialized as a String * @param nameSpaceAware determines whether the constructed Document * is name-space aware//from w w w . ja v a 2 s .c o m * @return Document */ public static Document stringToDocument(String xml, boolean nameSpaceAware) { Document document = null; try { if (xml != null) { StringReader reader = new StringReader(xml); InputSource input = new InputSource(reader); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(nameSpaceAware); factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(input); } } catch (Exception ex) { ex.printStackTrace(); } return document; }
From source file:Main.java
public static Document loadString(String paramString) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(false);/* ww w . ja va 2 s .com*/ factory.setIgnoringElementContentWhitespace(false); factory.setValidating(false); factory.setCoalescing(false); DocumentBuilder builder = factory.newDocumentBuilder(); char[] arrayOfChar = new char[paramString.length()]; paramString.getChars(0, paramString.length(), arrayOfChar, 0); InputSource input = new InputSource(new CharArrayReader(arrayOfChar)); return builder.parse(input); }
From source file:Main.java
public static Source getDomSourceIgnoringDtd(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // stop the loading of DTD files factory.setValidating(false); factory.setNamespaceAware(true);/*from www . j ava 2 s. co m*/ factory.setFeature("http://xml.org/sax/features/validation", false); factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); DocumentBuilder docbuilder = factory.newDocumentBuilder(); Document doc = docbuilder.parse(inputStream); Source domSource = new DOMSource(doc.getDocumentElement()); return domSource; }
From source file:Main.java
public static Document load(String path) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(false);/*from ww w. j a va2 s. c o m*/ factory.setIgnoringElementContentWhitespace(false); factory.setValidating(false); factory.setCoalescing(true); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(path); }
From source file:Main.java
public static Document load(File file) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(false);//from w w w . j a v a2 s. c o m factory.setIgnoringElementContentWhitespace(false); factory.setValidating(false); factory.setCoalescing(true); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(file); }