List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
/** * Convert XML Node to String/*from w w w .j a va 2s.c o m*/ * * @param node * the node to convert * @return the String equivalent of the node * @throws TransformerException */ public static String nodeToString(final Node node) throws TransformerException { final Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); final Writer out = new StringWriter(); tf.transform(new DOMSource(node), new StreamResult(out)); return out.toString(); }
From source file:Main.java
public static String sourceToXMLString(Source result) { String xmlResult = null;/*from w w w . ja v a 2 s .co m*/ try { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); StringWriter out = new StringWriter(); StreamResult streamResult = new StreamResult(out); transformer.transform(result, streamResult); xmlResult = streamResult.getWriter().toString(); } catch (TransformerException e) { e.printStackTrace(); } return xmlResult; }
From source file:Main.java
/** * Gets the node as raw./*from w ww .ja va 2 s . co m*/ * * @param pNode * the p node * @param encoding * the encoding * @param pRemoveXmlLine * the p remove xml line * @return the node as raw * @throws Exception * the exception */ public static byte[] getNodeAsRaw(Node pNode, String encoding, boolean pRemoveXmlLine) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); StreamResult result = new StreamResult(outputStream); transformNode(pNode, encoding, pRemoveXmlLine, result); return outputStream.toByteArray(); }
From source file:Main.java
public static void printDOM(Node node, OutputStream out, String encoding) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer;//from w ww. j a va 2 s .co m transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(node), new StreamResult(out)); }
From source file:Main.java
public static String getXMLString(Element elm, boolean htmlMode) throws TransformerException { /* TODO: Certain HTML entities, like "⇒", currently seem impossible to represent, as the HTML mode will output them in a named form unsupported by the Swing HTML parser (e.g. "⇐"). *///from ww w . j a v a 2s . c o m Transformer t; t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.METHOD, htmlMode ? "html" : "xml"); t.setOutputProperty(OutputKeys.INDENT, "no"); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, htmlMode ? "yes" : "no"); StringWriter ret = new StringWriter(); t.transform(new DOMSource(elm), new StreamResult(ret)); return ret.toString(); }
From source file:Main.java
/** * Serialized a node to a string. XML declarations will be omitted. * //from w w w . ja v a 2 s . c o m * @param node The node to be serialized. * * @return The serialized node. * * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static String nodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty("omit-xml-declaration", "yes"); StringWriter sw = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
/** * Writes a DOM document to a file./*from w ww.ja va2s . c o m*/ * * @param document * DOM document. * @param fileName * target file path. */ public static void writeDOMToFile(Document document, String fileName) { FileOutputStream fos; Source source; Result result; Transformer xformer; try { fos = new FileOutputStream(fileName); source = new DOMSource(document); result = new StreamResult(fos); xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static String getContent(Node node, boolean omitXMLDeclaration) { try {/*w w w.ja v a2s . c o m*/ ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty("encoding", "UTF-8"); if (omitXMLDeclaration) { transformer.setOutputProperty("omit-xml-declaration", "yes"); } DOMSource source = new DOMSource(node); StreamResult result = new StreamResult(baos); transformer.transform(source, result); String cont = baos.toString("UTF8"); baos.close(); return cont; } catch (Exception ex) { return ""; } }
From source file:Main.java
public static void writeXmlFile(Document doc, String filename) { try {//from ww w.j a v a 2s. co m // Prepare the DOM document for writing Source source = new DOMSource(doc); // Prepare the output file File file = new File(filename); Result result = new StreamResult(file); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); } catch (TransformerConfigurationException ex) { System.out.println(ex.getMessage()); } catch (TransformerException ex) { System.out.println(ex.getMessage()); } }
From source file:Main.java
/** * Uses a TransformerFactory with an identity transformation to convert a * Document into a String representation of the XML. * * @param document Document./*from w w w.ja v a 2 s .c om*/ * @return An XML String. * @throws IOException if an error occurs during transformation. */ public static String documentToString(Document document) throws IOException { String xml = null; try { DOMSource dom = new DOMSource(document); StringWriter writer = new StringWriter(); StreamResult output = new StreamResult(writer); // Use Transformer to serialize a DOM TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); // No need for pretty printing transformer.setOutputProperty(OutputKeys.INDENT, INDENT_XML); // XML Declarations unexpected whitespace for legacy AS XMLDocument type, // so we always omit it. We can't tell whether one was present when // constructing the Document in the first place anyway... transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, OMIT_XML_DECLARATION); transformer.transform(dom, output); xml = writer.toString(); } catch (TransformerException te) { throw new IOException("Error serializing Document as String: " + te.getMessageAndLocation()); } return xml; }