List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
public static String printXMLNode(Node node) { try {// www. j a va 2 s. c o m TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", 2); Transformer transformer = tf.newTransformer(); String omitDeclaration = node instanceof Document || node == node.getOwnerDocument().getDocumentElement() ? "no" : "yes"; transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(writer)); String output = writer.getBuffer().toString(); output = removeEmptyLines(output); // .replaceAll("\n|\r", ""); return output; } catch (TransformerException te) { throw new RuntimeException(te); } }
From source file:Main.java
public static String toXmlString(Document document) { StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); Transformer transformer;/*from www . ja v a 2 s . c om*/ try { transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(document.getLastChild()), result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return sw.toString(); }
From source file:Main.java
public static String nodeToString(Node node) throws TransformerException { StringWriter sw = new StringWriter(); Transformer t = TransformerFactory.newInstance().newTransformer(); t.transform(new DOMSource(node), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static String getFormattedXml(String xmlString) { // /////////////////////////////////////////////////////////////// // Declarations // /////////////////////////////////////////////////////////////// Source xmlInput = null;/*from w ww. j a v a 2 s. co m*/ StringWriter stringWriter = null; StreamResult xmlOutput = null; TransformerFactory transformerFactory = null; Transformer transformer = null; String formattedXml = null; // /////////////////////////////////////////////////////////////// // Code // /////////////////////////////////////////////////////////////// try { xmlInput = new StreamSource(new StringReader(xmlString)); stringWriter = new StringWriter(); xmlOutput = new StreamResult(stringWriter); transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 4); transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); formattedXml = xmlOutput.getWriter().toString(); } catch (Exception e) { // To Do: Handle Exception.. } return formattedXml; }
From source file:Main.java
public static void writeXmlFile(Document doc, File file) throws TransformerConfigurationException, TransformerException { Source source = new DOMSource(doc); Result result = new StreamResult(file); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(source, result); }
From source file:Main.java
/** * Returns the XML string representation of a Node. * @param node Node to convert into XML string. * @param omitXmlDeclaration <tt>true</tt> to remove the XML declaration from the string. * @return The XML string representation of the input node. * @throws TransformerConfigurationException * @throws TransformerException /*from www . j av a 2 s .c o m*/ */ public static String getXMLString(Node node, boolean omitXmlDeclaration) throws TransformerConfigurationException, TransformerException { StringWriter writer = new StringWriter(); DOMSource domSource = new DOMSource(node); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no"); serializer.transform(domSource, result); return (writer.toString()); }
From source file:Main.java
public static void writeDocument(Document document, OutputStream out) throws TransformerException { TransformerFactory transformFactory = TransformerFactory.newInstance(); Transformer idTransform = transformFactory.newTransformer(); Source input = new DOMSource(document); Result output = new StreamResult(out); idTransform.transform(input, output); }
From source file:Main.java
public static final void writeXML(Document doc, OutputStream out) throws TransformerException { final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer serializer = tf.newTransformer(); serializer.transform(new DOMSource(doc), new StreamResult(out)); }
From source file:Main.java
public static void print(Document doc, Writer out) throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(out); transformer.transform(source, result); }
From source file:Main.java
public static byte[] documentToByteArray(Document data, Integer indent) throws TransformerException { ByteArrayOutputStream result = new ByteArrayOutputStream(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); if (indent != null) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", indent.toString()); }//from w ww . j a va2 s . com transformer.transform(new DOMSource(data), new StreamResult(result)); return result.toByteArray(); }