List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
public static String formatXML(String unformatted) throws SAXException, IOException, TransformerException, ParserConfigurationException { if (unformatted == null) return null; // remove whitespaces between xml tags String unformattedNoWhiteSpaces = unformatted.toString().replaceAll(">[\\s]+<", "><"); // Instantiate transformer input Source xmlInput = new StreamSource(new StringReader(unformattedNoWhiteSpaces)); StreamResult xmlOutput = new StreamResult(new StringWriter()); // Configure transformer Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(xmlInput, xmlOutput); String formatted = xmlOutput.getWriter().toString(); return formatted; }
From source file:Main.java
public static void writeXMLDocToFile(Document outputDoc, String outFileName) { try {/*from w w w . j av a2 s .co m*/ //FileWriter writer = new FileWriter( outFileName ); final String encoding = "UTF-8"; OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outFileName), encoding); // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(outputDoc); StreamResult result = new StreamResult(writer); transformer.transform(source, result); writer.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("Could not create output XML file: " + outFileName); } catch (TransformerConfigurationException tce) { // Error generated by the parser System.out.println("* Transformer Factory error"); System.out.println(" " + tce.getMessage()); // Use the contained exception, if any Throwable x = tce; if (tce.getException() != null) x = tce.getException(); x.printStackTrace(); } catch (TransformerException te) { // Error generated by the parser System.out.println("* Transformation error"); System.out.println(" " + te.getMessage()); // Use the contained exception, if any Throwable x = te; if (te.getException() != null) x = te.getException(); x.printStackTrace(); } }
From source file:Main.java
/** * Output an XML document to the given file. * //from ww w . j a v a2s . c om * @param doc XML document to write. * @param filename File to write to. */ public static void writeXmlDocument(Document doc, String filename) { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); DOMSource source = new DOMSource(doc); final FileOutputStream out = new FileOutputStream(new File(filename)); StreamResult result = new StreamResult(out); transformer.transform(source, result); out.close(); } catch (IOException e) { LOGGER.warning("Error closing stream or could open a file from writing an XML document."); e.printStackTrace(); } catch (TransformerConfigurationException e) { LOGGER.warning("TransformerConfigurationException thrown when writing XML document to a file."); e.printStackTrace(); } catch (TransformerException e) { LOGGER.warning("Transformer exception thrown when writing XML document to a file."); e.printStackTrace(); } }
From source file:Main.java
public static String formatXmlAsString(Document document) { StringWriter writer = new StringWriter(); TransformerFactory factory = TransformerFactory.newInstance(); try {//from w w w . jav a 2 s .c o m factory.setAttribute("indent-number", new Integer(2)); } catch (IllegalArgumentException ex) { } try { Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(document), new StreamResult(writer)); } catch (TransformerException ex) { throw new RuntimeException("Error formatting xml as pretty-printed string", ex); } return writer.toString(); }
From source file:Main.java
/** * Creates a string representation of a {@link Node} instance. This method * does not introduce any character to the string representation of the * {@link Node} (eg. \n or \r characters) * * @param node A {@link Node} instance/* w w w . j a v a 2 s .co m*/ * @return A string representation of the node instance * @throws RequestSecurityTokenException */ public static String xmlToString(Node node) throws Exception { try { Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); throw new Exception("Cannot build a string representation of the assertion."); } catch (TransformerException e) { e.printStackTrace(); throw new Exception("Cannot build a string representation of the assertion."); } }
From source file:Main.java
public static org.w3c.dom.Document writeToFile(String xmlContent, String path) { System.out.println("This is the path " + path); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder;//from w w w.ja va2s . c o m try { builder = factory.newDocumentBuilder(); org.w3c.dom.Document doc = builder.parse(new InputSource(new StringReader(xmlContent))); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(path)); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); 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"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }
From source file:Main.java
public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty("omit-xml-declaration", "no"); transformer.setOutputProperty("method", "xml"); transformer.setOutputProperty("indent", "yes"); transformer.setOutputProperty("encoding", "UTF-8"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }
From source file:Main.java
public static void buildXmlFile(Document doc, File file) { TransformerFactory tfactory = TransformerFactory.newInstance(); try {//from ww w . ja v a 2 s .c om Transformer transformer = tfactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(file); transformer.setOutputProperty("encoding", "gb2312"); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
/** * DOM to string./*from w w w . j a v a 2 s.co m*/ * * @param doc * the doc * @return the string */ /* * from: * http://www.journaldev.com/71/utility-java-class-to-format-xml-document * -to-xml-string-and-xml-to-document */ public static String DOMToString(Document doc) { String xmlString = ""; if (doc != null) { try { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result); xmlString = sw.toString(); } catch (Exception e) { e.printStackTrace(); } } return xmlString; }