List of usage examples for javax.xml.transform Transformer transform
public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException;
Transform the XML Source
to a Result
.
From source file:Main.java
public static void write(Document doc, Writer writer) throws TransformerConfigurationException, TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(writer); transformer.transform(source, result); }
From source file:Main.java
public static String transformToString(Source source) { try {// w ww. jav a 2 s . c o m Transformer transformer = TransformerFactory.newInstance().newTransformer(); StreamResult result = new StreamResult(new StringWriter()); transformer.transform(source, result); return result.getWriter().toString(); } catch (TransformerException ex) { ex.printStackTrace(); return null; } }
From source file:Main.java
public static String convertDocumentToString(Document dom) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; try {// w ww.ja va 2 s . c o m transformer = tf.newTransformer(); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(dom), new StreamResult(writer)); String output = writer.getBuffer().toString(); return output; } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static void saveDocument(Document doc, File file) throws TransformerConfigurationException, TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(file); transformer.transform(source, result); }
From source file:com.mlo55.utils.XMLUtils.java
/** * Prettifies XML data, e.g. add indentation. * @throws Exception/*from w w w . jav a 2 s . co m*/ */ public static String formatXml(String result) throws Exception { Source source = new StringSource(result); Transformer transformer = getTransformer(); StreamResult streamResult = new StreamResult(new StringWriter()); transformer.transform(source, streamResult); final String xmlOut = streamResult.getWriter().toString(); return xmlOut; }
From source file:Main.java
/** Write a DOM to a file. */ public static void writeXML(Document document, File file) throws IOException { // save the DOM to file StreamResult result = new StreamResult(new BufferedOutputStream(new FileOutputStream(file))); TransformerFactory transFactory = TransformerFactory.newInstance(); try {/*from w ww . j a va 2 s . co m*/ Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); transformer.transform(new DOMSource(document), result); } catch (TransformerConfigurationException ex) { throw new IOException(ex.getMessage()); } catch (TransformerException ex) { throw new IOException(ex.getMessage()); } result.getOutputStream().close(); }
From source file:Main.java
/** * This method writes a DOM document to a file * @param filename/*from w w w.j a v a 2 s. c o m*/ * @param document */ public static void writeXmlToFile(String filename, Document document) { try { // Prepare the DOM document for writing Source source = new DOMSource(document); // Prepare the output file File file = new File(filename); Result result = new StreamResult(file); // Write the DOM document to the file // Get Transformer Transformer xformer = TransformerFactory.newInstance().newTransformer(); // Write to a file xformer.transform(source, result); } catch (TransformerConfigurationException e) { System.err.println("TransformerConfigurationException: " + e); } catch (TransformerException e) { System.err.println("TransformerException: " + e); } }
From source file:Main.java
public static void createFile(Document document, String filePath) throws TransformerException { TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); Source src = new DOMSource(document); Result dest = new StreamResult(new File(filePath)); aTransformer.transform(src, dest); }
From source file:Main.java
private static String nodeToString(Node node) throws Exception { StringWriter sw = new StringWriter(); Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
/** * Formats a xml string// w w w . j av a 2 s . co m * @param input * @param indent * @return */ public static String prettyFormatXml(String input, int indent) { try { Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { e.printStackTrace(); } return input; }