List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
/** * @see //http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java */// ww w . j ava2 s.c om public static String toXML(Document document, boolean format) throws TransformerException { if (format) { removeWhitespaceNodes(document.getDocumentElement()); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 2); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(out)); return out.toString(); }
From source file:Main.java
public static final String createStringFromDOMNode(Node node, boolean omitDeclaration) { assert node != null; StringWriter out = null;//from w ww . j a v a2s. c o m try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); node.normalize(); Source source = new DOMSource(node); out = new StringWriter(); Result resultStream = new StreamResult(out); if (omitDeclaration) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } else { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); } transformer.transform(source, resultStream); } catch (Exception e) { } if (out != null) { return out.toString(); } return null; }
From source file:Main.java
public static String pretty(String xml) { try {//from w w w . j a v a2 s . c o m Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StreamResult result = new StreamResult(new StringWriter()); Source source = new StreamSource(new StringReader(xml)); transformer.transform(source, result); return result.getWriter().toString().replace("\r\n", "\n").replace("\n\r", "\n").replace("\r", "\n"); } catch (TransformerFactoryConfigurationError | TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void saveDocument(Document document, String path) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerFactoryConfigurationError, TransformerException, IOException { StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); DOMSource dom = new DOMSource(document); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 4); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(dom, sr);//from ww w.j a va2 s.c om String string = sw.toString(); FileWriter fw = new FileWriter(new File(path)); fw.write(string); fw.close(); }
From source file:Main.java
public static String toXML(Document document, boolean format) throws Exception { if (format) { removeWhitespaceNodes(document.getDocumentElement()); }//from w w w . j a v a 2 s.co m TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 2); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(out)); return out.toString(); }
From source file:Main.java
public static String formatXML(String xml) { try {//from w w w .j a va 2 s.c o m Transformer serializer = SAXTransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes()))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); serializer.transform(xmlSource, res); return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray()); } catch (Exception e) { return xml; } }
From source file:Main.java
/** * // w ww .j a va 2 s . co m * comment : * @param doc * @param filename * @author Huynh Minh Duc */ public static void writeXmlFile(Document doc, String filename) { try { // Prepare the DOM document for writing Source source = new DOMSource(doc); // Prepare the output file File file = new File(filename); Result result = new StreamResult(file); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.transform(source, result); } catch (TransformerConfigurationException e) { } catch (TransformerException e) { } }
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:Main.java
public static void saveXml(Document doc, String filename) { try {//from ww w . j av a2 s .c o m TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(doc); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); PrintWriter pw = new PrintWriter(new FileOutputStream(filename)); StreamResult result = new StreamResult(pw); transformer.transform(source, result); } catch (TransformerException tfe) { tfe.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static void printXml(Document xml, Writer out) throws TransformerException, UnsupportedEncodingException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(xml), new StreamResult(out)); }