List of usage examples for javax.xml.transform.stream StreamResult getWriter
public Writer getWriter()
From source file:Main.java
/** * Method to get formatted Xml string from Xml source * * @param payload Source/*from ww w .ja v a 2s .c o m*/ * @return a formatted Xml string */ public static String getXmlStringFromSource(Source payload) { String result = null; StreamResult strResult = new StreamResult(new StringWriter()); if (payload != null) { try { TransformerFactory factory = TransformerFactory.newInstance(); //factory.setAttribute("indent-number", new Integer(2)); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1"); transformer.transform(payload, strResult); } catch (TransformerException e) { e.printStackTrace(); } result = strResult.getWriter().toString(); } return result; }
From source file:com.mobilefirst.fiberlink.WebServiceRequest.java
/** * prettyFormatXML translates the input xml and creates the indentation you would like to achieve * @param input: XML String/* w ww. jav a 2 s.c o m*/ * @param indent: Integer for how much indent to specify */ public static String prettyFormatXML(String input, Integer 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); javax.xml.transform.Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", indent.toString()); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String getFormattedXml(String xmlString) { // /////////////////////////////////////////////////////////////// // Declarations // /////////////////////////////////////////////////////////////// Source xmlInput = null;/*from ww w . j av a2s .c o 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:com.rapidminer.gui.OperatorDocLoader.java
/** * This loads the documentation of the operator referenced by the operator description from the * Wiki in the internet./*from w w w . j ava 2s . c o m*/ */ private static String loadSelectedOperatorDocuFromWiki(OperatorDescription opDesc) throws IOException, ParserConfigurationException, OperatorCreationException, TransformerException, URISyntaxException { String operatorWikiName = StringUtils.EMPTY; if (!opDesc.isDeprecated()) { operatorWikiName = opDesc.getName().replace(" ", "_"); if (opDesc.getProvider() != null) { String prefix = opDesc.getProvider().getPrefix(); prefix = Character.toUpperCase(prefix.charAt(0)) + prefix.substring(1); operatorWikiName = prefix + ":" + operatorWikiName; } Document documentOperator = parseDocumentForOperator(operatorWikiName, opDesc); if (documentOperator != null) { // writing html back to string Transformer transformer = TransformerFactory.newInstance().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(documentOperator); transformer.transform(source, result); String HTMLString = result.getWriter().toString(); HTMLString = customizeHTMLStringDirty(HTMLString, SwingTools.getIconPath("24/" + opDesc.getIconName())); return HTMLString; } } return loadSelectedOperatorDocuLocally(opDesc); }
From source file:com.azaptree.services.command.http.handler.CommandServiceHandlerTest.java
public static String prettyFormat(final String input, final int indent) { try {//from w w w. j a v a 2s . c o m final Source xmlInput = new StreamSource(new StringReader(input)); final StringWriter stringWriter = new StringWriter(); final StreamResult xmlOutput = new StreamResult(stringWriter); final TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); final Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (final Exception e) { throw new RuntimeException(e); // simple exception handling, please review it } }
From source file:org.apache.falcon.regression.core.util.Util.java
/** * Prints xml in readable form.//from w w w . j av a 2s . com * @param xmlString xmlString * @return formatted xmlString */ public static String prettyPrintXml(final String xmlString) { if (xmlString == null) { return null; } try { Source xmlInput = new StreamSource(new StringReader(xmlString)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", "2"); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (TransformerConfigurationException e) { return xmlString; } catch (TransformerException e) { return xmlString; } }
From source file:Main.java
/** * @param xml//from www . jav a 2 s .c o m */ public static String prettyPrintToString(String xml) { Source xmlInput = new StreamSource(new StringReader(xml)); StreamResult xmlOutput = new StreamResult(new StringWriter()); Transformer transformer = null; try { transformer = TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); try { transformer.transform(xmlInput, xmlOutput); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } String formattedxml = xmlOutput.getWriter().toString(); return formattedxml; }
From source file:Main.java
/** * @param xml/* w ww .j ava 2s . com*/ */ public static void prettyPrint(String xml) { Source xmlInput = new StreamSource(new StringReader(xml)); StreamResult xmlOutput = new StreamResult(new StringWriter()); Transformer transformer = null; try { transformer = TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); try { transformer.transform(xmlInput, xmlOutput); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } String formattedxml = xmlOutput.getWriter().toString(); System.out.println(formattedxml); }
From source file:it.intecs.pisa.toolbox.util.URLReader.java
public static Object getFullURLContent(String urlString) throws Exception { String returnString = ""; URL url = null;// w ww .j a v a 2s. c o m URLConnection connection = null; // Open Connection try { url = new URL(urlString); } catch (MalformedURLException ex) { ex.printStackTrace(); } try { connection = url.openConnection(); } catch (IOException ex) { ex.printStackTrace(); } HttpURLConnection httpConn = (HttpURLConnection) connection; try { StreamSource source = new StreamSource(httpConn.getInputStream()); TransformerFactory fac = TransformerFactory.newInstance(); Transformer trans = null; StreamResult res = new StreamResult(new StringWriter()); try { trans = fac.newTransformer(); } catch (TransformerConfigurationException ex) { ex.printStackTrace(); } try { trans.transform(source, res); } catch (TransformerException ex) { ex.printStackTrace(); } StringWriter w = (StringWriter) res.getWriter(); returnString = w.toString(); } catch (IOException ex) { ex.printStackTrace(); } return returnString; }
From source file:com.portfolio.data.utils.DomUtils.java
public static String processXSLTfile2String(Document xml, String xslFile, String param[], String paramVal[], StringBuffer outTrace) throws Exception { // ======================================= outTrace.append("<br>-->processXSLTfile2String-" + xslFile); Transformer transformer = TransformerFactory.newInstance() .newTransformer(new StreamSource(new File(xslFile))); outTrace.append(".1"); StreamResult result = new StreamResult(new StringWriter()); outTrace.append(".2"); DOMSource source = new DOMSource(xml); outTrace.append(".3"); for (int i = 0; i < param.length; i++) { outTrace.append("<br>setParemater - " + param[i] + ":" + paramVal[i] + "..."); transformer.setParameter(param[i], paramVal[i]); outTrace.append("ok"); }//from w ww.java 2 s .c om outTrace.append(".4"); transformer.transform(source, result); outTrace.append("<br><--processXSLTfile2String-" + xslFile); return result.getWriter().toString(); }