List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
private static void WriteXMLFile(Document doc, OutputStreamWriter w, String encoding) { try {/*from w w w . jav a2s . c om*/ Source source = new DOMSource(doc); Result ret = new StreamResult(w); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, encoding); xformer.transform(source, ret); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static String transform(String xmlString, String stylesheetPathname) throws Exception { try {//from w w w . j av a 2 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 Exception(tce.getMessageAndLocation()); } catch (TransformerException te) { throw new Exception(te.getMessageAndLocation()); } }
From source file:Main.java
public static void printNode(Node node) { try {//from w ww . ja va2 s .c o m // Set up the output transformer TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); // Print the DOM node StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(node); trans.transform(source, result); String xmlString = sw.toString(); System.out.println(xmlString); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static String XMLToString(Document doc) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.toString(); }
From source file:Main.java
public static String toXml(Object object) { final StringWriter out = new StringWriter(); JAXBContext context = null;/*from ww w . j ava 2 s. c o m*/ try { context = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(object, new StreamResult(out)); } catch (PropertyException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } return out.toString(); }
From source file:Main.java
public static String printNode(Node node) { String result = ""; try {//from ww w . j a v a 2 s. com TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty("method", "xml"); StringWriter sw = new StringWriter(); DOMSource source = new DOMSource(node); transformer.transform(source, new StreamResult(sw)); result = sw.toString(); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static void serializeDocument(Document doc, OutputStream outputStream) throws Exception { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer = tfactory.newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); serializer.transform(new DOMSource(doc), new StreamResult(outputStream)); }
From source file:Main.java
/** * @param dom/*from ww w . j a v a 2 s. c om*/ * @param outFile */ public static void writeDomToFile(Document dom, File outFile) { try { TransformerFactory transFact = TransformerFactory.newInstance(); Transformer transformer = transFact.newTransformer(); DOMSource source = new DOMSource(dom); StreamResult result; result = new StreamResult( new OutputStreamWriter(new FileOutputStream(outFile), System.getProperty("file.encoding"))); transformer.transform(source, result); } catch (Exception e) { throw new RuntimeException( "Could not write dom tree '" + dom.getBaseURI() + "' to file '" + outFile.getName() + "'!", e); } }
From source file:Main.java
public static String domNodeToString(Node node) throws TransformerException { StringWriter writer = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.toString(); }
From source file:Main.java
public static String getDOMString(Document doc) { String s = null;/*from ww w .j ava 2s.c o m*/ final TransformerFactory tfactory = TransformerFactory.newInstance(); try { final Transformer xform = tfactory.newTransformer(); final Source src = new DOMSource(doc); final StringWriter writer = new StringWriter(); final Result result = new StreamResult(writer); xform.transform(src, result); s = writer.toString(); } catch (final Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; }