List of usage examples for javax.xml.parsers DocumentBuilderFactory setNamespaceAware
public void setNamespaceAware(boolean awareness)
From source file:Main.java
public static final String[] executeXPathExpression(InputSource inputSource, String xpathExpression, String namespace) throws XPathExpressionException, SAXException, IOException, ParserConfigurationException, TransformerException { // optional namespace spec: xmlns:prefix:URI String nsPrefix = null;/*from w ww .ja v a 2 s . com*/ String nsUri = null; if (namespace != null && namespace.startsWith("xmlns:")) { String[] nsDef = namespace.substring("xmlns:".length()).split("="); if (nsDef.length == 2) { nsPrefix = nsDef[0]; nsUri = nsDef[1]; } } // Parse XML to DOM DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setNamespaceAware(true); Document doc = dbFactory.newDocumentBuilder().parse(inputSource); // Find nodes by XPATH XPathFactory xpFactory = XPathFactory.newInstance(); XPath xpath = xpFactory.newXPath(); // namespace? if (nsPrefix != null) { final String myPrefix = nsPrefix; final String myUri = nsUri; xpath.setNamespaceContext(new NamespaceContext() { public String getNamespaceURI(String prefix) { return myPrefix.equals(prefix) ? myUri : null; } public String getPrefix(String namespaceURI) { return null; // we are not using this. } public Iterator<?> getPrefixes(String namespaceURI) { return null; // we are not using this. } }); } XPathExpression expr = xpath.compile(xpathExpression); NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); List<String> lines = new ArrayList<>(); for (int i = 0; i < nodes.getLength(); i++) { lines.add((indenting(nodes.item(i)))); } return lines.toArray(new String[lines.size()]); }
From source file:Main.java
/** * Parse the content of the given file as an XML document and return a new DOM * //from w w w .j av a 2 s. co m * @param fileName * @return xml Document * @throws Exception */ public static Document readXmlFile(String fileName) throws Exception { Document doc = null; try { File xmlFile = new File(fileName); DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(false); DocumentBuilder builder = domFactory.newDocumentBuilder(); doc = builder.parse(xmlFile); } catch (ParserConfigurationException e) { throw new Exception(e.getMessage(), e); } catch (Exception e) { throw new Exception(e.getMessage(), e); } return doc; }
From source file:Main.java
public static Document getDocument(InputSource xml, InputStream xsd) throws ParserConfigurationException, SAXException, IOException { Document doc = null;//from www .j a va 2 s .c o m try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); if (xsd != null) { dbf.setNamespaceAware(true); } DocumentBuilder builder = dbf.newDocumentBuilder(); doc = builder.parse(xml); if (xsd != null) validateXml(doc, xsd); } finally { closeStream(xml.getByteStream()); } return doc; }
From source file:Main.java
/** * Creates and returns an new document builder factory. This method tries to * configure the namespace support for the builder. If the underlying parser * does not support namespaces then this method returns a simple * DocumentBuilder object./*from ww w . j a va 2s.co m*/ * * @return a new document builder * @throws ParserConfigurationException */ private static DocumentBuilder getDocumentBuilder(boolean isNamespaceAware) throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(isNamespaceAware); // never forget this! try { factory.setFeature("http://xml.org/sax/features/namespaces", isNamespaceAware); } catch (Throwable t) { // Just skip it... } DocumentBuilder builder = factory.newDocumentBuilder(); return builder; }
From source file:Main.java
public static DocumentBuilder documentBuilder() throws ParserConfigurationException { DocumentBuilderFactory dbf = DOCUMENT_BUILDER_FACTORY.get(); if (dbf == null) { dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DOCUMENT_BUILDER_FACTORY.set(dbf); }/*from w w w .j av a2s . c o m*/ return dbf.newDocumentBuilder(); }
From source file:net.bpelunit.test.util.TestUtil.java
public static Element parse(String toEncode) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader("<dummy>" + toEncode + "</dummy>"))); // should be one element Element dummy = (Element) document.getChildNodes().item(0); return dummy; }
From source file:Main.java
/** * Creates a non-validating DocumentBuilder with the following settings: * <ul>//w ww . j a v a2 s . c om * <li>Validating: false * <li>NamespaceAware: true * </ul> */ public static DocumentBuilder createNonValidatingDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setValidating(false); // if true, then need to also set an org.xml.sax.ErrorHandler on DocumentBuilder dbFactory.setNamespaceAware(true); DocumentBuilder result = dbFactory.newDocumentBuilder(); return result; }
From source file:be.fedict.eid.tsl.TrustServiceListFactory.java
private static Document parseDocument(File file) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(file); return document; }
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);/* w ww .j a va2s . co m*/ factory.setNamespaceAware(true); 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:cz.muni.fi.mir.mathmlunificator.utils.DOMBuilder.java
/** * Get W3C DOM document builder with common configuration set: most * importantly build is set to be namesapace aware. * * @return W3C DOM document builder with common configuration. * @throws ParserConfigurationException//from w w w .j av a2 s .c om */ public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setNamespaceAware(true); return docFactory.newDocumentBuilder(); }