List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
public static void save(Document doc, File file) { save(doc, new StreamResult(file)); }
From source file:Main.java
public static String prettyFormat(String input, int indent) { try {//from w w w . ja v a 2 s . c om 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) { throw new RuntimeException(e); // simple exception handling, please review it } }
From source file:Main.java
public static String getStringFromDocument(Document doc) throws TransformerException { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); return writer.toString(); }
From source file:Main.java
/** * Formats an unformatted xml and returns a pretty format. * //from w ww . j ava2s. c om * @param unformattedXml * the unformatted xml string * @return the xml in a pretty format * @throws TransformerException */ public static String formatXml(String unformattedXml) throws TransformerException { Document doc = parseXml(unformattedXml); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // initialize StreamResult with File object to save to file StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); String xmlString = result.getWriter().toString(); return xmlString; }
From source file:Util.java
/** * Applies a stylesheet to a given xml document. * // w w w .j a v a 2s . co m * @param xmlDocument * the xml document to be transformed * @param xsltFilename * the filename of the stylesheet * @return the transformed xml document * @throws Exception */ public static String transformDocumentAsString(Document xmlDocument, String xsltFilename) throws Exception { // Generate a Transformer. Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFilename)); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); // Perform the transformation. transformer.transform(new DOMSource(xmlDocument), streamResult); // Now you can get the output Node from the DOMResult. return stringWriter.toString(); }
From source file:Main.java
public static String formatDOM(Node node) throws TransformerException { StringWriter writer = new StringWriter(); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer;/*from w w w. j a va 2 s .com*/ transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // encoding doesn't matter since we stick with strings transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.toString(); }
From source file:Main.java
public static String elementToString(final Node node) throws Exception { final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final Document document = factory.newDocumentBuilder().newDocument(); final Node importedNode = document.importNode(node, true); document.appendChild(importedNode);/* www . j a va2 s. c om*/ final ByteArrayOutputStream bos = new ByteArrayOutputStream(); transformer.transform(new DOMSource(document), new StreamResult(bos)); return bos.toString("UTF-8"); }
From source file:Main.java
public static void prettyFormatXml(final InputStream xml, final OutputStream os, final int indent) { try {//from ww w . java2s.c om final Source xmlInput = new StreamSource(xml); final StreamResult xmlOutput = new StreamResult(os); final TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); final Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); } catch (final Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
/** Write a DOM document to an OutputStream */ public static void writeXmlDocumentToStream(Document document, OutputStream stream) throws Exception { Source source = new DOMSource(document); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); StreamResult result = new StreamResult(stream); transformer.transform(source, result); stream.flush();//from w w w . ja v a 2 s. co m }
From source file:Main.java
/** * Converts XML document into the string. * @param document XML/* ww w . jav a 2s. com*/ * @return XML as string * @throws TransformerException */ public static String toString(Document document) throws TransformerException { DOMSource domSource = new DOMSource(document); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); return writer.toString(); }