List of usage examples for javax.xml.transform Transformer setOutputProperty
public abstract void setOutputProperty(String name, String value) throws IllegalArgumentException;
From source file:Utils.java
public static void writeXml(Node n, OutputStream os) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); // identity// www. j av a2 s .com Transformer t = tf.newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(new DOMSource(n), new StreamResult(os)); }
From source file:Main.java
public static String nodeAsString(Node node) { String nodeStr = ""; TransformerFactory tff = TransformerFactory.newInstance(); try {//from w w w . ja v a2 s. c o m Transformer tf = tff.newTransformer(); tf.setOutputProperty("encoding", "UTF-8"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); tf.transform(new DOMSource(node), new StreamResult(bos)); return bos.toString(); } catch (Exception e) { e.printStackTrace(); } return nodeStr; }
From source file:Main.java
public static String printDocument(Node doc) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StringWriter sw = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static String xmlToString(Document doc) { try {// ww w. j a v a2s . co m 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)); String output = writer.getBuffer().toString().replaceAll("\n|\r", ""); return output; } catch (TransformerException exc) { throw new RuntimeException("Unable to convert XML to String"); } }
From source file:Main.java
public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty("omit-xml-declaration", "no"); transformer.setOutputProperty("method", "xml"); transformer.setOutputProperty("indent", "yes"); transformer.setOutputProperty("encoding", "UTF-8"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }
From source file:Main.java
public static String toXMLString(Node node) { if (node == null) { return ""; }/* www . j av a 2s .c o m*/ try { Transformer tran = tf.newTransformer(); tran.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter swriter = new StringWriter(); Source src = new DOMSource(node); Result res = new StreamResult(swriter); tran.transform(src, res); return swriter.getBuffer().toString(); } catch (Exception e) { e.printStackTrace(); return ""; } }
From source file:Main.java
public static OutputStream streamDocument(Document doc) throws IOException, TransformerException { OutputStream out = null;//from ww w. jav a 2 s .c o m TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty("omit-xml-declaration", "no"); transformer.setOutputProperty("method", "xml"); transformer.setOutputProperty("indent", "yes"); transformer.setOutputProperty("encoding", "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); return out; }
From source file:Main.java
public static boolean write(Document doc, String path) { boolean result = false; try {/* w w w.ja va 2s . c om*/ DOMSource inputDoc = new DOMSource(doc); StreamResult sr = new StreamResult(path); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(inputDoc, sr); result = true; } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static String asString(Document doc) { try {//from w w w . jav a2s . co m 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.getBuffer().toString(); } catch (TransformerException e) { throw Throwables.propagate(e); } }
From source file:Main.java
public static String DocumentToStr(Document doc) throws TransformerException { StringWriter sw = new StringWriter(); TransformerFactory tf = TransformerFactory.newInstance(); 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(); }