List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
static public void outputToStream(Document document, OutputStream outputStream) throws Exception { // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); // transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(outputStream); transformer.transform(source, result); }
From source file:Main.java
public static String documentToString(Document document) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer;/* www . j a v a 2 s. c o m*/ try { transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); try { transformer.transform(new DOMSource(document), new StreamResult(writer)); } catch (TransformerException e) { throw new RuntimeException(e); } return writer.getBuffer().toString().replaceAll("\n|\r", ""); }
From source file:Main.java
public static String nodeToString(final Node node) { final TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer;/* w w w . j a va 2 s . c om*/ try { transformer = transFactory.newTransformer(); final StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(buffer)); buffer.append("\n"); return buffer.toString(); } catch (final Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String serializeElement(Element element) throws TransformerFactoryConfigurationError, TransformerException { String serializedElement = null; if (element != null) { StringWriter output = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(element), new StreamResult(output)); serializedElement = output.toString(); }//from w w w. ja va2 s . co m return serializedElement; }
From source file:Main.java
public static void saveXMLDocument(Document document, File file) { FileOutputStream output = null; try {// w w w.j av a2 s.c o m output = new FileOutputStream(file); Source source = new DOMSource(document); Result result = new StreamResult(output); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(source, result); } catch (Exception ex) { try { output.close(); } catch (IOException io) { } } finally { try { output.close(); } catch (IOException io) { } } }
From source file:Main.java
/** * Converts an XML {@link org.w3c.dom.Document} to a string. * * @param doc the XML document/*from ww w . j a va2 s . co m*/ * @return the string representation of the XML document */ public static String documentToString(Document doc) { try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.getBuffer().toString(); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(System.err); } return ""; }
From source file:Main.java
public static String newStringFromDocument(Document doc) throws TransformerConfigurationException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty("omit-xml-declaration", "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString().replaceAll("\n|\r", ""); return output; }
From source file:Main.java
/** * Converts a DOM document to an xml String. * * @param doc DOM document//from w ww . j a va2 s . c o m * @return xml String. * @throws TransformerException */ public static String getStringFromDomDocument(org.w3c.dom.Document doc, org.w3c.dom.Document xslt) throws TransformerException { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; if (null != xslt) { DOMSource dxsltsource = new DOMSource(xslt); transformer = tf.newTransformer(dxsltsource); } else { transformer = tf.newTransformer(); } transformer.transform(domSource, result); return writer.toString(); }
From source file:Main.java
public static String nodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException { if (node == null) return ""; StringWriter sw = new StringWriter(); Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static String getXMLString(Node doc) throws TransformerException, TransformerConfigurationException { TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); StringWriter sw = new StringWriter(); Source src = new DOMSource(doc); Result dest = new StreamResult(sw); aTransformer.transform(src, dest);//w w w . j a va2s. co m return sw.toString(); }