List of usage examples for javax.xml.transform Transformer setOutputProperty
public abstract void setOutputProperty(String name, String value) throws IllegalArgumentException;
From source file:Main.java
/** * Transform xml Document to String/*from www. jav a 2 s . co m*/ * @param doc Xml Document * @return String representation */ public static String getContent(final Document doc) { try { final StringWriter sw = new StringWriter(); final TransformerFactory tf = TransformerFactory.newInstance(); final 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.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); } catch (Exception ex) { throw new RuntimeException("Error converting to String", ex); } }
From source file:Main.java
public static String createPrintableString(Document doc) { String s = ""; try {/*from w w w . j a v a 2 s . co m*/ // set up a transformer TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); // create string from xml tree StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result); s = sw.toString(); } catch (Exception e) { e.printStackTrace(); } return s; }
From source file:Main.java
public static String writeXmlToStream(Document doc, Result streamResult) { String result = ""; try {/*from ww w . java 2s . co m*/ // Prepare the DOM document for writing Source source = new DOMSource(doc); // Write the DOM document to the file TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer xformer = tFactory.newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); xformer.transform(source, streamResult); } catch (TransformerConfigurationException e) { } catch (TransformerException e) { } return result; }
From source file:Main.java
public static void saveDocument(Document dom, String file) throws TransformerException, IOException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, dom.getDoctype().getPublicId()); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dom.getDoctype().getSystemId()); DOMSource source = new DOMSource(dom); StreamResult result = new StreamResult(); FileOutputStream outputStream = null; try {//from ww w.java 2 s. co m outputStream = new FileOutputStream(file); result.setOutputStream(outputStream); transformer.transform(source, result); outputStream.flush(); } finally { if (outputStream != null) { outputStream.close(); } } }
From source file:Main.java
public static void doc2stream(Document doc, StreamResult result) throws TransformerConfigurationException, TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(doc); transformer.transform(source, result); }
From source file:Main.java
public static String printNode(Node node) { String result = ""; try {/*from w ww.j a va2 s . c o m*/ 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 String nodeToString(Node node) { StringWriter sw = new StringWriter(); try {/* w w w. jav a2s. co m*/ Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "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 void saveDocumentToFile(Document xmlDoc, File file) throws TransformerConfigurationException, TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource source = new DOMSource(xmlDoc); StreamResult result = new StreamResult(file); transformer.transform(source, result); }
From source file:Main.java
private static void writeDo(Document doc, Writer w, String encoding) throws TransformerFactoryConfigurationError, TransformerException { Source source = new DOMSource(doc); Result result = new StreamResult(w); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, encoding); xformer.transform(source, result);/* w ww .j a v a 2 s . c om*/ }
From source file:Main.java
/** * Returns the XML string representation of a Node. * @param node Node to convert into XML string. * @param omitXmlDeclaration <tt>true</tt> to remove the XML declaration from the string. * @return The XML string representation of the input node. * @throws TransformerConfigurationException * @throws TransformerException /*from w ww . j av a 2 s .c o m*/ */ public static String getXMLString(Node node, boolean omitXmlDeclaration) throws TransformerConfigurationException, TransformerException { StringWriter writer = new StringWriter(); DOMSource domSource = new DOMSource(node); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no"); serializer.transform(domSource, result); return (writer.toString()); }