List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
/** Write an XML DOM tree to a file * * @param doc the document to write/* www.ja v a 2 s . c om*/ * @param file the file to write to * @throws IOException if a file I/O error occurs */ public static void write(Document doc, File file) throws IOException { try { DOMSource domSource = new DOMSource(doc); FileOutputStream stream = new FileOutputStream(file); // OutputStreamWriter works around indent bug 6296446 in JDK // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6296446 StreamResult streamResult = new StreamResult(new OutputStreamWriter(stream)); TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", new Integer(2)); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); } catch (TransformerException e) { // This exception is never thrown, treat as fatal if it is throw new RuntimeException(e); } }
From source file:Main.java
public static Node xsltTransform(InputStream styleSheet, Document response, Map<String, String> params) { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = null; try {/* w w w.j a v a 2s . c o m*/ javax.xml.transform.stream.StreamSource streamSource = new javax.xml.transform.stream.StreamSource( styleSheet); transformer = tFactory.newTransformer(streamSource); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (Iterator<String> iterator = params.keySet().iterator(); iterator.hasNext();) { String key = iterator.next(); String value = params.get(key); transformer.setParameter(key, value); } Source xmlSource = new DOMSource(response); DOMResult result = new DOMResult(); try { transformer.transform(xmlSource, result); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result.getNode(); }
From source file:Main.java
/** * src:http://www.journaldev.com/1237/java-convert-string-to-xml-document-and-xml-document-to-string * @param doc//from w ww. j ava2 s . com * @return */ public static String convertDocumentToString(Document doc) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; try { transformer = tf.newTransformer(); // below code to remove XML declaration // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString(); return output; } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Converts a DOM document to an xml String. * * @param doc DOM document/*from w w w . j a v a 2 s. com*/ * @return xml String. * @throws TransformerException */ public static String getStringFromDomDocument(org.w3c.dom.Document doc, org.w3c.dom.Document xslt) throws TransformerException { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; if (null != xslt) { DOMSource dxsltsource = new DOMSource(xslt); transformer = tf.newTransformer(dxsltsource); } else { transformer = tf.newTransformer(); } transformer.transform(domSource, result); return writer.toString(); }
From source file:Main.java
/** * Output an XML document to the given file. * /* w w w . j a va 2 s . c om*/ * @param doc XML document to write. * @param filename File to write to. */ public static void writeXmlDocument(Document doc, String filename) { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); DOMSource source = new DOMSource(doc); final FileOutputStream out = new FileOutputStream(new File(filename)); StreamResult result = new StreamResult(out); transformer.transform(source, result); out.close(); } catch (IOException e) { LOGGER.warning("Error closing stream or could open a file from writing an XML document."); e.printStackTrace(); } catch (TransformerConfigurationException e) { LOGGER.warning("TransformerConfigurationException thrown when writing XML document to a file."); e.printStackTrace(); } catch (TransformerException e) { LOGGER.warning("Transformer exception thrown when writing XML document to a file."); e.printStackTrace(); } }
From source file:Main.java
public static String transform(String xml, String stylesheet) throws TransformerException { StringWriter writer = new StringWriter(); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(stylesheet)); transformer.transform(new StreamSource(xml), new StreamResult(writer)); return writer.toString(); }
From source file:Main.java
/** * Convert XML Document to a String.//from w w w. jav a2s .c om * * @param node * @return */ public static String xmlToString(Node node) { try { Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:Main.java
private static String getIndented(Document aDoc) { String outputXml = ""; try {/*from w ww . ja v a 2 s.c o m*/ TransformerFactory tf = TransformerFactory.newInstance(); // tf.setAttribute("indent-number", new Integer(4)); Transformer transformer; transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(aDoc); transformer.transform(source, result); outputXml = result.getWriter().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return outputXml; }
From source file:Main.java
public static void writeXMLDocToFile(Document outputDoc, String outFileName) { try {/*ww w . j av a2 s . c om*/ //FileWriter writer = new FileWriter( outFileName ); final String encoding = "UTF-8"; OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outFileName), encoding); // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(outputDoc); StreamResult result = new StreamResult(writer); transformer.transform(source, result); writer.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("Could not create output XML file: " + outFileName); } catch (TransformerConfigurationException tce) { // Error generated by the parser System.out.println("* Transformer Factory error"); System.out.println(" " + tce.getMessage()); // Use the contained exception, if any Throwable x = tce; if (tce.getException() != null) x = tce.getException(); x.printStackTrace(); } catch (TransformerException te) { // Error generated by the parser System.out.println("* Transformation error"); System.out.println(" " + te.getMessage()); // Use the contained exception, if any Throwable x = te; if (te.getException() != null) x = te.getException(); x.printStackTrace(); } }
From source file:Main.java
public static String getXMLAsString(Document doc) throws Exception { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); return result.getWriter().toString(); }