public static String format(String xmlStr) { // Instantiate transformer input Source xmlInput = new StreamSource(new StringReader(xmlStr)); StreamResult xmlOutput = new StreamResult(new StringWriter()); // Configure transformer Transformer transformer; try { transformer = TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException e) { logger.error(e.getMessage(), e); return xmlStr; } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block logger.error(e.getMessage(), e); return xmlStr; } // An identity transformer try { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(xmlInput, xmlOutput); } catch (TransformerException e) { logger.error(e.getMessage(), e); return xmlStr; } return xmlOutput.getWriter().toString(); }
public void write(HttpServletResponse response) { StreamResult streamResult; SAXTransformerFactory tf; TransformerHandler hd; Transformer serializer; try { try { streamResult = new StreamResult(response.getWriter()); tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); hd = tf.newTransformerHandler(); serializer = hd.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); serializer.setOutputProperty( OutputKeys.DOCTYPE_SYSTEM, "http://labs.omniti.com/resmon/trunk/resources/resmon.dtd"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); hd.setResult(streamResult); hd.startDocument(); AttributesImpl atts = new AttributesImpl(); hd.startElement("", "", "ResmonResults", atts); for (ResmonResult r : s) { r.write(hd); } hd.endElement("", "", "ResmonResults"); hd.endDocument(); } catch (TransformerConfigurationException tce) { response.getWriter().println(tce.getMessage()); } catch (SAXException se) { response.getWriter().println(se.getMessage()); } } catch (IOException ioe) { } }
private void message(String text, HttpServletResponse response, PublishAsType publishAs) { ServletOutputStream out; String xmlText; Server.logger.errorLogEntry(text); try { xmlText = "<?xml version = \"1.0\" encoding=\"utf-8\"?><request><error type=\"" + type + "\">" + "<message><version>" + Server.serverVersion + "</version><errortext>" + XMLUtil.getAsTagValue(text) + "</errortext></message></error></request>"; // System.out.println("xml text = "+xmlText); response.setHeader( "Cache-Control", "no-cache, must-revalidate, private, no-store, s-maxage=0, max-age=0"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); if (publishAs == PublishAsType.HTML) { response.setContentType("text/html;charset=utf-8"); out = response.getOutputStream(); Source xmlSource = new StreamSource(new StringReader(xmlText)); Result result = new StreamResult(out); TransformerFactory transFact = TransformerFactory.newInstance(); Transformer trans = transFact.newTransformer(xsltSource); // System.out.println(PortalEnv.appID+": xsl transformation="+PortalEnv.errorXSL); trans.transform(xmlSource, result); } else { response.setContentType("text/xml;charset=utf-8"); // response.sendError(550); out = response.getOutputStream(); out.println(xmlText); } } catch (IOException ioe) { System.out.println(ioe); ioe.printStackTrace(); } catch (TransformerConfigurationException tce) { System.out.println(tce); tce.printStackTrace(); } catch (TransformerException te) { System.out.println(te); te.printStackTrace(); } }
/** * Writes an XML file from a Document object. * * @param doc the Document object to be written to file * @param file the file to be written * @throws IOException * @author Klaus Meffert * @since 2.0 */ public static void writeFile(Document doc, File file) throws IOException { // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer; try { transformer = tFactory.newTransformer(); } catch (TransformerConfigurationException tex) { throw new IOException(tex.getMessage()); } DOMSource source = new DOMSource(doc); FileOutputStream fos = new FileOutputStream(file); StreamResult result = new StreamResult(fos); try { transformer.transform(source, result); fos.close(); } catch (TransformerException tex) { throw new IOException(tex.getMessage()); } }
public static boolean writeXML(File file, Document document) { javax.xml.transform.Transformer transformer = null; try { transformer = TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); return false; } transformer.setOutputProperty("indent", "yes"); transformer.setOutputProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "2"); transformer.setOutputProperty("encoding", "UTF-8"); try { transformer.transform(new DOMSource(document), new StreamResult(file)); } catch (TransformerException e) { e.printStackTrace(); return false; } return true; }