List of usage examples for javax.xml.parsers DocumentBuilderFactory setNamespaceAware
public void setNamespaceAware(boolean awareness)
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);// w w w . j a v a 2 s.c om Document doc = factory.newDocumentBuilder().parse(file); return doc; }
From source file:Main.java
public static void validateXml(InputStream xml, InputStream xsd) throws SAXException, IOException, ParserConfigurationException { try {// ww w . jav a 2 s . c o m DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document doc = dbf.newDocumentBuilder().parse(xml); validateXml(doc, xsd); } finally { closeStream(xml); closeStream(xsd); } }
From source file:Main.java
/** * Gets the node list from the given xml file and the given xpath expression. * //from w w w . j av a2 s.c o m * @param xml * the xml * @param xpathExpression * the xpath expression * @return the node list * @throws XPathExpressionException * the x path expression exception * @throws ParserConfigurationException * the parser configuration exception * @throws SAXException * the sAX exception * @throws IOException * Signals that an I/O exception has occurred. */ public static NodeList getNodeList(final File xml, final String xpathExpression) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException { final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); final DocumentBuilder builder = domFactory.newDocumentBuilder(); final Document doc = builder.parse(xml); final XPath xpath = XPathFactory.newInstance().newXPath(); final XPathExpression expr = xpath.compile(xpathExpression); final Object result = expr.evaluate(doc, XPathConstants.NODESET); final NodeList nodes = (NodeList) result; return nodes; }
From source file:Main.java
/** * Gets the node list from the given xml file and the given xpath expression. * //from w w w . ja v a2 s .c om * @param xml * the xml file as string. * @param xpathExpression * the xpath expression as string. * @return the node list * @throws XPathExpressionException * the x path expression exception * @throws ParserConfigurationException * the parser configuration exception * @throws SAXException * the sAX exception * @throws IOException * Signals that an I/O exception has occurred. */ public static NodeList getNodeList(final String xml, final String xpathExpression) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException { final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); final DocumentBuilder builder = domFactory.newDocumentBuilder(); final Document doc = builder.parse(xml); final XPath xpath = XPathFactory.newInstance().newXPath(); final XPathExpression expr = xpath.compile(xpathExpression); final Object result = expr.evaluate(doc, XPathConstants.NODESET); final NodeList nodes = (NodeList) result; return nodes; }
From source file:Main.java
/** * Applies a stylesheet (that receives parameters) to a given xml document. * * @param xmlDocument the xml document to be transformed * @param parameters the hashtable with the parameters to be passed to the * stylesheet/*from w ww . j av a 2s. co m*/ * @param xsltFilename the filename of the stylesheet * @return the transformed xml document * @throws Exception */ public static Document transformDocument(Document xmlDocument, Map<String, String> parameters, String xsltFilename) throws TransformerException, ParserConfigurationException { File xslFile = new File(xsltFilename); if (xslFile.exists()) { // Generate a Transformer. Transformer transformer = TransformerFactory.newInstance() .newTransformer(new StreamSource(xsltFilename)); if (transformer != null) { // set transformation parameters if (parameters != null) { for (Map.Entry<String, String> param : parameters.entrySet()) { transformer.setParameter(param.getKey(), param.getValue()); } } // Create an empty DOMResult object for the output. DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); dFactory.setNamespaceAware(true); DocumentBuilder dBuilder = dFactory.newDocumentBuilder(); Document dstDocument = dBuilder.newDocument(); DOMResult domResult = new DOMResult(dstDocument); // Perform the transformation. transformer.transform(new DOMSource(xmlDocument), domResult); // Now you can get the output Node from the DOMResult. return dstDocument; } } return null; }
From source file:Main.java
public static DocumentBuilderFactory getDocumentBuilderFactory(final boolean namespaceAware, boolean disableDtdXxe) { final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); if (namespaceAware) { documentBuilderFactory.setNamespaceAware(namespaceAware); }//from w w w. j av a2s. c om if (disableDtdXxe) { setDocBuilderFeature(documentBuilderFactory, "http://xerces.apache.org/xerces-j/features.html#external-general-entities", false); setDocBuilderFeature(documentBuilderFactory, "http://xerces.apache.org/xerces2-j/features.html#external-general-entities", false); setDocBuilderFeature(documentBuilderFactory, "http://xml.org/sax/features/external-general-entities", false); setDocBuilderFeature(documentBuilderFactory, "http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl", true); setDocBuilderFeature(documentBuilderFactory, "http://apache.org/xml/features/disallow-doctype-decl", true); } return documentBuilderFactory; }
From source file:Main.java
/** * Builds new DocumentBuilder//from w w w. java 2 s.c om * * @return DocumentBuilder * @throws CitationStyleManagerException */ public static DocumentBuilder createDocumentBuilder() { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); dbf.setIgnoringComments(true); dbf.setNamespaceAware(false); try { return dbf.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new RuntimeException("Failed to create a document builder factory", e); } }
From source file:Main.java
/** * Create from factory a DocumentBuilder and let it create a org.w3c.dom.Document. * This method takes InputSource. After successful finish the document tree is returned. * * @param input a parser input (for URL users use: <code>new InputSource(url.toExternalForm())</code> * @param validate if true validating parser is used * @param namespaceAware if true DOM is created by namespace aware parser * @param errorHandler a error handler to notify about exception or <code>null</code> * @param entityResolver SAX entity resolver or <code>null</code>; see class Javadoc for hints * * @throws IOException if an I/O problem during parsing occurs * @throws SAXException is thrown if a parser error occurs * @throws FactoryConfigurationError Application developers should never need to directly catch errors of this type. * * @return document representing given input, or null if a parsing error occurs *//*w w w . ja v a2 s . c o m*/ public static Document parse(InputSource input, boolean validate, boolean namespaceAware, ErrorHandler errorHandler, EntityResolver entityResolver) throws IOException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validate); factory.setNamespaceAware(namespaceAware); DocumentBuilder builder = null; try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException ex) { throw new SAXException("Cannot create parser satisfying configuration parameters", ex); //NOI18N } if (errorHandler != null) { builder.setErrorHandler(errorHandler); } if (entityResolver != null) { builder.setEntityResolver(entityResolver); } return builder.parse(input); }
From source file:Main.java
private static Element loadRootElement(InputStream inputStream) throws Exception { // Get XML document from the URL DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //as a default, set validating to false factory.setValidating(false);/*from www . ja v a 2 s . c o m*/ factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource inputSource = new InputSource(inputStream); Document doc = builder.parse(inputSource); return doc.getDocumentElement(); }
From source file:eu.delving.x3ml.X3MLCommandLine.java
static DocumentBuilderFactory documentBuilderFactory() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); return factory; }