List of usage examples for javax.xml.parsers DocumentBuilderFactory setAttribute
public abstract void setAttribute(String name, Object value) throws IllegalArgumentException;
From source file:Main.java
public static Document CreateDocument(Path xml, Path xsd) throws SAXException, IOException, ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//from ww w . ja va2s . com factory.setNamespaceAware(true); factory.setIgnoringComments(true); factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); if (xsd != null) { factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xsd.toString()); } return factory.newDocumentBuilder().parse(xml.toString()); }
From source file:Main.java
private static DocumentBuilder getDocumentBuilder(boolean NamespaceAwareness) throws ParserConfigurationException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(NamespaceAwareness); //domFactory.setValidating(true); //domFactory.setIgnoringElementContentWhitespace( true ); domFactory.setAttribute("http://apache.org/xml/features/dom/include-ignorable-whitespace", Boolean.FALSE); return domFactory.newDocumentBuilder(); }
From source file:Main.java
private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true);//w ww .j a v a2 s.c o m // Unfortunately we can not ignore whitespaces without a schema. // So we use the NdLst workaround for now. domFactory.setAttribute("http://apache.org/xml/features/dom/include-ignorable-whitespace", Boolean.FALSE); return domFactory.newDocumentBuilder(); }
From source file:Main.java
public static DocumentBuilder getDocumentBuilder(boolean NamespaceAwareness) throws ParserConfigurationException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(NamespaceAwareness); // Unfortunately we can not ignore whitespaces without a schema. // So we use the NdLst workaround for now. //domFactory.setValidating(true); //domFactory.setIgnoringElementContentWhitespace( true ); domFactory.setAttribute("http://apache.org/xml/features/dom/include-ignorable-whitespace", Boolean.FALSE); return domFactory.newDocumentBuilder(); }
From source file:Main.java
public static Document createDocumentFromFile(String pathToXmlFile) throws ParserConfigurationException, SAXException, IOException { // path to file is global String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; // String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); // validate against XML Schema in dbsql2xml.xsd // documentBuilderFactory.setNamespaceAware(true); //INFO change validation to true. Someday when xsd file is complete... documentBuilderFactory.setValidating(false); documentBuilderFactory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); // documentBuilderFactory.setAttribute(JAXP_SCHEMA_SOURCE, new File(pathToXsdFile)); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(new File(pathToXmlFile)); return document; }
From source file:com.microsoft.tfs.util.xml.JAXPUtils.java
/** * <p>// ww w.j a va2s .com * Creates a new (or configures an existing) {@link DocumentBuilderFactory} * that will perform XML Schema validation when parsing. This method is * called before parsing to obtain a configured * {@link DocumentBuilderFactory} that produces {@link DocumentBuilder}s * that will be used for XML Schema for validation. * </p> * * <p> * The supplied <code>schemaSource</code> object must be one of the * following: * <ul> * <li>A {@link String} that points to the URI of the schema</li> * * <li>An {@link InputStream} with the schema contents (will not be closed * by this method)</li> * * <li>A SAX {@link InputSource} that indicates the schema</li> * * <li>A {@link File} that indicates the schema</li> * * <li>An array of objects, each one of which is one of the above</li> * </ul> * </p> * * @throws XMLException * if the {@link DocumentBuilderFactory} can't be created or * properly configured * * @param factory * the {@link DocumentBuilderFactory} to configure, or * <code>null</code> to create a {@link DocumentBuilderFactory} using * the {@link #newDocumentBuilderFactory()} method * @param schemaSource * the schema source as described above * @return a configured {@link DocumentBuilderFactory} (never * <code>null</code>) */ public static DocumentBuilderFactory newDocumentBuilderFactoryForXSValidation(DocumentBuilderFactory factory, final Object schemaSource) { if (factory == null) { factory = newDocumentBuilderFactory(); } factory.setNamespaceAware(true); factory.setValidating(true); try { factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); } catch (final IllegalArgumentException e) { final String messageFormat = "The DocumentBuilderFactory [{0}] loaded from ClassLoader [{1}] does not support JAXP 1.2"; //$NON-NLS-1$ final String message = MessageFormat.format(messageFormat, factory.getClass().getName(), factory.getClass().getClassLoader()); throw new XMLException(message, e); } if (schemaSource != null) { factory.setAttribute(JAXP_SCHEMA_SOURCE, schemaSource); } return factory; }
From source file:com.impetus.kundera.ejb.PersistenceXmlLoader.java
/** * Gets the document./*from w w w. j a va 2 s . c o m*/ * * @param configURL * the config url * @return the document * @throws Exception * the exception */ private static Document getDocument(URL configURL) throws Exception { InputStream is = null; if (configURL != null) { URLConnection conn = configURL.openConnection(); conn.setUseCaches(false); // avoid JAR locking on Windows and Tomcat is = conn.getInputStream(); } if (is == null) { throw new IOException("Failed to obtain InputStream from url: " + configURL); } DocumentBuilderFactory docBuilderFactory = null; docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setValidating(true); docBuilderFactory.setNamespaceAware(true); try { // otherwise Xerces fails in validation docBuilderFactory.setAttribute("http://apache.org/xml/features/validation/schema", true); } catch (IllegalArgumentException e) { docBuilderFactory.setValidating(false); docBuilderFactory.setNamespaceAware(false); } InputSource source = new InputSource(is); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); // docBuilder.setEntityResolver( resolver ); List errors = new ArrayList(); docBuilder.setErrorHandler(new ErrorLogger("XML InputStream", errors)); Document doc = docBuilder.parse(source); if (errors.size() != 0) { throw new PersistenceException("invalid persistence.xml", (Throwable) errors.get(0)); } is.close(); //Close input Stream return doc; }
From source file:com.connexta.arbitro.ctx.InputParser.java
/** * Tries to Parse the given output as a Context document. * //w ww. j av a 2 s . c o m * @param input the stream to parse * @param rootTag either "Request" or "Response" * * @return the root node of the request/response * * @throws ParsingException if a problem occurred parsing the document */ public static Node parseInput(InputStream input, String rootTag) throws ParsingException { NodeList nodes = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(true); DocumentBuilder builder = null; // as of 1.2, we always are namespace aware factory.setNamespaceAware(true); if (ipReference == null) { // we're not validating factory.setValidating(false); builder = factory.newDocumentBuilder(); } else { // we are validating factory.setValidating(true); factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); factory.setAttribute(JAXP_SCHEMA_SOURCE, ipReference.schemaFile); builder = factory.newDocumentBuilder(); builder.setErrorHandler(ipReference); } Document doc = builder.parse(input); nodes = doc.getElementsByTagName(rootTag); } catch (Exception e) { throw new ParsingException("Error tring to parse " + rootTag + "Type", e); } if (nodes.getLength() != 1) throw new ParsingException("Only one " + rootTag + "Type allowed " + "at the root of a Context doc"); return nodes.item(0); }
From source file:com.vmware.qe.framework.datadriven.utils.XMLUtil.java
/** * Creates the parser instance based on dom parser factory Validates the xml file against the * XSD schema Return the documentElement of the xml document if validations succeeded * /*from w w w.j a va 2s . com*/ * @param xmlFile - XML file to be parsed * @param xmlSchema - XSD that XML file should be validated against * @return documentElement of the XML file */ public static Document getXmlDocumentElement(String xmlFile, String xmlSchema) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setCoalescing(true); factory.setNamespaceAware(false); factory.setIgnoringElementContentWhitespace(true); factory.setValidating(true); factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", XMLUtil.class.getResource(xmlSchema).getFile()); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new org.xml.sax.helpers.DefaultHandler()); // Parse the document from the classpath. URL xmlFileUri = XMLUtil.class.getResource(xmlFile); if (null == xmlFileUri) { log.error("Unable to find file on classpath: " + xmlFile); return null; } return builder.parse(xmlFileUri.getFile()); }
From source file:fr.cls.atoll.motu.library.misc.xml.XMLUtils.java
/** * Validate xml./*w w w. j av a2 s. c om*/ * * @param inSchemas the in schemas * @param inXml the in xml * @param schemaLanguage the schema language * * @return the xML error handler * * @throws MotuException the motu exception */ public static XMLErrorHandler validateXML(String[] inSchemas, String inXml, String schemaLanguage) throws MotuException { XMLErrorHandler errorHandler = new XMLErrorHandler(); try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); // Must enable namespace processing!!!!! try { documentBuilderFactory.setXIncludeAware(true); } catch (Exception e) { // Do Nothing } documentBuilderFactory.setExpandEntityReferences(true); documentBuilderFactory.setAttribute(XMLUtils.JAXP_SCHEMA_LANGUAGE, schemaLanguage); // final String[] srcSchemas = // {"http://schemas.opengis.net/iso/19139/20060504/srv/serviceMetadata.xsd", // }; // final String[] srcSchemas = // {"http://opendap.aviso.oceanobs.com/data/ISO_19139/srv/serviceMetadata.xsd", // "http://opendap.aviso.oceanobs.com/data/ISO_19139/gco/gco.xsd", }; // C:\Documents and Settings\dearith\Mes documents\Atoll\SchemaIso\gml // final String[] srcSchemas = // {"C:/Documents and Settings/us/userocuments/Atoll/SchemaIso/srv/serviceMetadata.xsd", // }; // final String[] srcSchemas = {"schema/iso/srv/serviceMetadata.xsd", // }; documentBuilderFactory.setAttribute(XMLUtils.JAXP_SCHEMA_SOURCE, inSchemas); // URL url = Organizer.findResource("schema/iso/srv/srv.xsd"); // URL url = Organizer.findResource("iso/19139/20070417/srv/serviceMetadata.xsd"); // documentBuilderFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", // url.toString()); documentBuilderFactory.setValidating(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); // document = documentBuilder.parse(new File(xmlUrl.toURI())); documentBuilder.setErrorHandler(errorHandler); documentBuilder.parse(inXml); } catch (Exception e) { throw new MotuException(e); // instance document is invalid! } return errorHandler; }