List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
/** * output XML data in a file//from w w w.j a va 2 s . c om * * @param doc * @param path */ public static void buildXmlFile(Document doc, String path) { TransformerFactory tfactory = TransformerFactory.newInstance(); try { Transformer transformer = tfactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(path)); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("encoding", "UTF-8"); transformer.transform(source, result); } catch (TransformerConfigurationException e) { //TODO } catch (TransformerException e) { } }
From source file:Main.java
public static String nodeToString(Node node) { StringWriter sw = new StringWriter(); try {/* w ww . java 2 s .co m*/ Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (TransformerException te) { throw new RuntimeException(te); } return sw.toString(); }
From source file:Main.java
public static String prettyPrintXML(String xml) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); StreamSource source = new StreamSource(new StringReader(xml)); transformer.transform(source, result); return result.getWriter().toString(); }
From source file:Main.java
private static void finalizeXML(File xmlFile, Document doc, int intendAmount) throws Exception { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(xmlFile); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "" + intendAmount); transformer.setOutputProperty("indent", "yes"); transformer.transform(source, result); }
From source file:Main.java
public static String transform(String xmlString, String stylesheetPathname) throws TransformerException { try {/* www . ja v a2 s.c o m*/ TransformerFactory factory = TransformerFactory.newInstance(); Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile()); Transformer transformer = factory.newTransformer(stylesheetSource); Source inputSource = new StreamSource(new StringReader(xmlString)); Writer outputWriter = new StringWriter(); Result outputResult = new StreamResult(outputWriter); transformer.transform(inputSource, outputResult); return outputWriter.toString(); } catch (TransformerConfigurationException tce) { throw new TransformerException(tce.getMessageAndLocation()); } catch (TransformerException te) { throw new TransformerException(te.getMessageAndLocation()); } }
From source file:Main.java
/** * Formatte un XML/*w w w . ja va2 s.co m*/ * */ 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(); 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) { 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:Main.java
public static void saveXML(Document paramDocument, File paramFile) throws TransformerException { TransformerFactory localTransformerFactory = TransformerFactory.newInstance(); Transformer localTransformer = localTransformerFactory.newTransformer(); localTransformer.setOutputProperty("indent", "yes"); StreamResult localStreamResult = new StreamResult(paramFile); DOMSource localDOMSource = new DOMSource(paramDocument); localTransformer.transform(localDOMSource, localStreamResult); }
From source file:Main.java
public static void createFile(Document document, String filePath) throws TransformerException { TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); Source src = new DOMSource(document); Result dest = new StreamResult(new File(filePath)); aTransformer.transform(src, dest);//w ww .ja v a 2s . com }
From source file:Main.java
public static String transformToString(Document doc) throws TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); transformer.transform(source, result); return writer.toString(); }
From source file:Main.java
public static void doSerialize(Document d, File f) throws TransformerException, IOException { Transformer t = tf.newTransformer(); FileOutputStream fos = new FileOutputStream(f); StreamResult sr = new StreamResult(fos); t.transform(new DOMSource(d), sr); fos.close();//from ww w. j a va 2s .c o m }