List of usage examples for javax.xml.parsers DocumentBuilderFactory setNamespaceAware
public void setNamespaceAware(boolean awareness)
From source file:Main.java
/** * Parses a XML document./*from w w w .j a v a2 s .c o m*/ * * @param xml the XML document * @return DOM */ public static Document parse(String xml) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(xml))); removeWhitespaces((Element) document.getChildNodes().item(0)); return document; }
From source file:Main.java
/** * @param parent//from w ww .j a va2s. c o m * node to add fragment to * @param fragment * a well formed XML fragment * @throws ParserConfigurationException */ public static void appendXmlFragment(Node parent, String fragment) throws IOException, SAXException, ParserConfigurationException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); DocumentBuilder docBuilder = domFactory.newDocumentBuilder(); Document doc = parent.getOwnerDocument(); Node fragmentNode = docBuilder.parse(new InputSource(new StringReader(fragment))).getDocumentElement(); fragmentNode = doc.importNode(fragmentNode, true); parent.appendChild(fragmentNode); }
From source file:Main.java
/** * ***************************************** * Load XML document from string/*ww w . j av a 2s .c o m*/ * ******************************************. * * @param xmlString the xml string * @return the document * @throws Exception the exception */ public static Document loadDocument(String xmlString) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource source = new InputSource(new StringReader(xmlString)); Document doc = db.parse(source); return doc; }
From source file:Main.java
/** * Create a document builder factory./*from ww w .j a v a2 s.c o m*/ * * @return */ private static DocumentBuilderFactory getDOMFactory() { DocumentBuilderFactory dbf; try { dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); } catch (Exception e) { dbf = null; } return dbf; }
From source file:Main.java
public static ArrayList<Node> getNodesXPath(String srcXmlString, String xPath) { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(false); // never forget this! Document doc = null;/*from w w w . ja v a 2 s . c om*/ DocumentBuilder builder = null; ArrayList<Node> nodesList = new ArrayList<Node>(); try { builder = domFactory.newDocumentBuilder(); doc = builder.parse(new ByteArrayInputStream(srcXmlString.getBytes())); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile(xPath); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList xPathNodes = (NodeList) result; logger.debug("xpath result count: " + xPathNodes.getLength()); // iterate through all the nodes for (int i = 0; i < xPathNodes.getLength(); i++) { nodesList.add(xPathNodes.item(i)); } } catch (Exception ex) { logger.error(ex.getMessage()); } return nodesList; }
From source file:Main.java
/** * Parse a DOM Document from the given XML file. * /* ww w. ja v a2s . com*/ * @param f File to parse as Document * @return Document * @throws IOException */ public static Document getDocument(File f) throws IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); // never forget this! try { DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(f); } catch (ParserConfigurationException e) { IOException ioe = new IOException(); ioe.initCause(e); throw ioe; } catch (SAXException e) { IOException ioe = new IOException(); ioe.initCause(e); throw ioe; } }
From source file:Main.java
public static String extractValue(String xml, String xpathExpression) { String actual;/*from ww w . j a v a 2s .c om*/ try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilderFactory.setIgnoringElementContentWhitespace(true); DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder(); byte[] bytes = xml.getBytes("UTF-8"); InputStream inputStream = new ByteArrayInputStream(bytes); Document doc = docBuilder.parse(inputStream); XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xpath = xPathFactory.newXPath(); actual = xpath.evaluate(xpathExpression, doc, XPathConstants.STRING).toString(); } catch (Exception e) { throw new RuntimeException(e); } return actual; }
From source file:Main.java
private static DocumentBuilderFactory getDOMFactory() { DocumentBuilderFactory dbf; try {/*from ww w .ja va 2 s . c o m*/ dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); } catch (Exception e) { //log.error(Messages.getMessage("exception00"), e ); dbf = null; } return (dbf); }
From source file:Main.java
/** * Creates a new document object from the given input stream containing * the document XML./* w w w. j a v a 2 s .co m*/ * @param documentXml the input stream containing the XML * @param namespaceAware flag indicating namespace awareness * @return the created document object * @throws Exception if an error occurs */ public static Document parseDocument(InputStream documentXml, boolean namespaceAware) throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(namespaceAware); return documentBuilderFactory.newDocumentBuilder().parse(documentXml); }
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 ww w . j a va2 s .co 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; }