List of usage examples for javax.xml.transform.stream StreamResult getWriter
public Writer getWriter()
From source file:ru.retbansk.utils.UsefulMethods.java
/** * For testing usage. Take unformatted xml string. Return pretty readable xml string * /*from ww w . j av a 2 s . c o m*/ * @param input take unformatted xml string * @param indent a number of white spaces before each line * @return pretty readable xml string */ public static String prettyFormat(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(); // This statement works with JDK 6 transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Throwable e) { // You'll come here if you are using JDK 1.5 // you are getting an the following exeption // java.lang.IllegalArgumentException: Not supported: indent-number // Use this code (Set the output property in transformer. try { Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent)); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Throwable t) { return input; } } }
From source file:com.portfolio.data.utils.DomUtils.java
private static String printDOM(Document doc) throws Exception { // ======================================= Transformer transformer = TransformerFactory.newInstance().newTransformer(); StreamResult result = new StreamResult(new StringWriter()); transformer.transform(new DOMSource(doc), result); return result.getWriter().toString(); }
From source file:com.petercho.Encoder.java
public static String formatXml(String xml) throws TransformerException { Source xmlInput = new StreamSource(new StringReader(xml)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 4); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); }
From source file:com.igormaznitsa.upom.UPomModel.java
private static String findAndRemoveDuplicatedSiblings(final Log log, final String xmlText) throws Exception { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final Document xmldoc = builder.parse(new InputSource(new StringReader(xmlText))); insideElementJanitor(log, xmldoc, new ArrayList<String>()); final TransformerFactory tFactory = TransformerFactory.newInstance(); final Transformer transformer = tFactory.newTransformer(); final DOMSource source = new DOMSource(xmldoc); final StringWriter buffer = new StringWriter(xmlText.length()); final StreamResult result = new StreamResult(buffer); transformer.transform(source, result); return ((StringWriter) result.getWriter()).toString(); }
From source file:Main.java
/** * Pretty format a given XML document//from w ww . j a v a2 s . c om * * @param strInput * Valid XML document (No validity check yet!) * @param nIndent * Indent * @return Formatted XML document * @throws Exception * in error case */ public static String prettyFormat(String strInput, int nIndent) throws Exception { try { Source xmlInput = new StreamSource(new StringReader(strInput)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", nIndent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(nIndent)); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { // Logger.XMLEval.logState("Pretty formatting: " + e.getMessage(), LogLevel.Error); throw e; } }
From source file:com.sdl.odata.renderer.util.PrettyPrinter.java
/** * Pretty-print a given XML./* w ww . java 2 s . c om*/ * * @param xml The not-formatted XML. * @return The pretty-printed XML. */ public static String prettyPrintXml(String xml) throws TransformerException, IOException { Source xmlInput = new StreamSource(new StringReader(xml)); try (StringWriter stringWriter = new StringWriter()) { StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", DEFAULT_INDENT); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name()); transformer.setOutputProperty(OutputKeys.VERSION, "1.0"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } }
From source file:com.portfolio.data.utils.DomUtils.java
private static String dom2string(Document dom) throws Exception { // supprimer // ======================================= Transformer transformer = TransformerFactory.newInstance().newTransformer(); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(dom); transformer.transform(source, result); return result.getWriter().toString(); }
From source file:Main.java
public static String toStringFromDoc(Element elem) { String result = null;//w w w .j av a 2s . c o m StringWriter strWtr = new StringWriter(); StreamResult strResult = new StreamResult(strWtr); TransformerFactory tfac = TransformerFactory.newInstance(); try { javax.xml.transform.Transformer t = tfac.newTransformer(); t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html, // text t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); t.transform(new DOMSource(elem), strResult); } catch (Exception e) { System.err.println("XML.toString(Document): " + e); } result = strResult.getWriter().toString(); try { strWtr.close(); } catch (IOException e) { e.printStackTrace(); } return result; }
From source file:com.code19.library.L.java
private static void printXml(String tag, String xml, String headString) { if (TextUtils.isEmpty(tag)) { tag = TAG;/* w ww .jav a 2 s . com*/ } if (xml != null) { try { Source xmlInput = new StreamSource(new StringReader(xml)); StreamResult xmlOutput = new StreamResult(new StringWriter()); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(xmlInput, xmlOutput); xml = xmlOutput.getWriter().toString().replaceFirst(">", ">\n"); } catch (Exception e) { e.printStackTrace(); } xml = headString + "\n" + xml; } else { xml = headString + "Log with null object"; } printLine(tag, true); String[] lines = xml.split(LINE_SEPARATOR); for (String line : lines) { if (!TextUtils.isEmpty(line)) { Log.d(tag, "|" + line); } } printLine(tag, false); }
From source file:hydrograph.ui.common.util.XMLUtil.java
/** * /*from ww w . j av a 2 s . c o m*/ * Format given XML string * * @param xmlString * @return String */ public static String formatXML(String xmlString) { try (Writer writer = new StringWriter()) { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(TRANSFORMER_INDENT_AMOUNT_KEY, INDENT_SPACE); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); //initialize StreamResult with File object to save to file StreamResult result = new StreamResult(writer); Document xmlDoc = convertStringToDocument(xmlString); if (xmlDoc == null) { return xmlString; } DOMSource source = new DOMSource(xmlDoc); transformer.transform(source, result); return result.getWriter().toString(); } catch (TransformerException e) { logger.debug("Unable to format XML string", e); } catch (IOException e) { logger.debug("Unable to format XML string", e); } return null; }