List of usage examples for javax.xml.parsers DocumentBuilderFactory setValidating
public void setValidating(boolean validating)
From source file:Main.java
/** * Convert the byte array representation of an Element into an Element * @param byte[] representation of an Element. * @param boolean set whether the return Element should be namespace aware. * @return Element// ww w . j ava2s . c om */ public static Element byteArrayToElement(byte[] b, boolean namespaceAware) throws ParserConfigurationException, SAXException, UnsupportedEncodingException, IOException { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(namespaceAware); docBuilderFactory.setValidating(false); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(new ByteArrayInputStream(b)); return doc.getDocumentElement(); }
From source file:Main.java
private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringComments(true);/*from w w w . j ava 2 s.c om*/ dbf.setCoalescing(true); dbf.setIgnoringElementContentWhitespace(true); dbf.setValidating(false); return dbf.newDocumentBuilder(); }
From source file:Main.java
/** * @return/* ww w. j a va 2 s.com*/ * @throws ParserConfigurationException */ public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { if (parser == null) { // JPT: Remove xerces use DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilderFactory.setValidating(false); parser = documentBuilderFactory.newDocumentBuilder(); } return parser; }
From source file:gov.nih.nci.cabig.caaers.utils.XmlValidator.java
public static boolean validateAgainstSchema(String xmlContent, String xsdUrl, StringBuffer validationResult) { boolean validXml = false; try {//from w w w . j a v a 2s . c o m // parse an XML document into a DOM tree DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setValidating(false); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder(); Document document = parser.parse(new InputSource(new StringReader(xmlContent))); // create a SchemaFactory capable of understanding WXS schemas SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // load a WXS schema, represented by a Schema instance Source schemaFile = new StreamSource(getResources(xsdUrl)[0].getFile()); Schema schema = schemaFactory.newSchema(schemaFile); // create a Validator instance, which can be used to validate an instance document Validator validator = schema.newValidator(); // validate the DOM tree validator.validate(new DOMSource(document)); validXml = true; } catch (FileNotFoundException ex) { throw new CaaersSystemException("File Not found Exception", ex); } catch (IOException ioe) { validationResult.append(ioe.getMessage()); logger.error(ioe.getMessage()); } catch (SAXParseException spe) { validationResult.append("Line : " + spe.getLineNumber() + " - " + spe.getMessage()); logger.error("Line : " + spe.getLineNumber() + " - " + spe.getMessage()); } catch (SAXException e) { validationResult.append(e.toString()); logger.error(e.toString()); } catch (ParserConfigurationException pce) { validationResult.append(pce.getMessage()); } return validXml; }
From source file:Main.java
/** * Parse the XML file and create Document * @param fileName//w w w .j av a 2s . c o m * @return Document */ public static Document parse(InputStream fs) { Document document = null; // Initiate DocumentBuilderFactory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // To get a validating parser factory.setValidating(false); // To get one that understands namespaces factory.setNamespaceAware(true); try { // Get DocumentBuilder DocumentBuilder builder = factory.newDocumentBuilder(); // Parse and load into memory the Document //document = builder.parse( new File(fileName)); document = builder.parse(fs); return document; } catch (SAXParseException spe) { // Error generated by the parser System.err.println("\n** Parsing error , line " + spe.getLineNumber() + ", uri " + spe.getSystemId()); System.err.println(" " + spe.getMessage()); // Use the contained exception, if any Exception x = spe; if (spe.getException() != null) x = spe.getException(); x.printStackTrace(); } catch (SAXException sxe) { // Error generated during parsing Exception x = sxe; if (sxe.getException() != null) x = sxe.getException(); x.printStackTrace(); } catch (ParserConfigurationException pce) { // Parser with specified options can't be built pce.printStackTrace(); } catch (IOException ioe) { // I/O error ioe.printStackTrace(); } return null; }
From source file:Main.java
public static Document parseStreamToXML(InputStream in) { try {// w w w.jav a 2 s.co 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().parse(in); } catch (ParserConfigurationException errParser) { throw new RuntimeException("Error getting XML parser", errParser); } catch (SAXException errSax) { throw new RuntimeException("Error parsing XML files", errSax); } catch (IOException errIO) { throw new RuntimeException("Error parsing XML files", errIO); } }
From source file:Main.java
public static Document parseStreamToXML(Reader in) { try {/*from w w w.j a va2 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().parse(new InputSource(in)); } catch (ParserConfigurationException errParser) { throw new RuntimeException("Error getting XML parser", errParser); } catch (SAXException errSax) { throw new RuntimeException("Error parsing XML files", errSax); } catch (IOException errIO) { throw new RuntimeException("Error parsing XML files", errIO); } }
From source file:Main.java
public static Document parseXmlFile(File file, boolean validating, boolean namespaceAware) throws SAXException, IOException, ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(namespaceAware); factory.setValidating(validating); Document doc = factory.newDocumentBuilder().parse(file); return doc;/*from w ww . j a v a2 s . c o m*/ }
From source file:Main.java
public static Document loadString(String domContent) throws Exception { javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(false);// ww w .j a v a 2 s .c o m factory.setIgnoringElementContentWhitespace(false); factory.setValidating(false); factory.setCoalescing(false); DocumentBuilder builder = factory.newDocumentBuilder(); char[] chars = new char[domContent.length()]; domContent.getChars(0, domContent.length(), chars, 0); InputSource is = new InputSource(new CharArrayReader(chars)); return (builder.parse(is)); }
From source file:Main.java
/** * Creates a new {@link DocumentBuilder} object. *//*w w w .ja v a 2 s . 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(); }