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
public static synchronized boolean update(String sourceXML, Document doc) { try {/*w w w.j a v a 2s .c o m*/ Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, ENCODING); tf.setOutputProperty(OutputKeys.INDENT, INDENT); Writer out = new FileWriter(new File(sourceXML)); tf.transform(new DOMSource(doc), new StreamResult(out)); return true; } catch (TransformerException | IOException e) { e.printStackTrace(); } return false; }
From source file:Main.java
public static void printDocument(Document doc, OutputStream out, boolean prettyPrint) throws IOException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); if (prettyPrint) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); }/*from ww w . j ava2s . c o m*/ transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }
From source file:Main.java
/** Returns the entire value of a node (child nodes and text) as a String * @param node// w ww . jav a2s .c o m * @return */ public static String nodeChildrenToString(Node node) { StringWriter sw = new StringWriter(); try { Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) t.transform(new DOMSource(nl.item(i)), new StreamResult(sw)); } catch (TransformerException te) { System.out.println("nodeToString Transformer Exception"); } return sw.toString().trim(); }
From source file:Main.java
/** * Convert a DOM tree into a String using transform * @param domDoc DOM object * @throws java.io.IOException I/O exception * @return XML as String *//*from ww w .j a va2 s . c o m*/ public static String docToString2(Document domDoc) throws IOException { try { TransformerFactory transFact = TransformerFactory.newInstance(); Transformer trans = transFact.newTransformer(); trans.setOutputProperty(OutputKeys.INDENT, "no"); StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); trans.transform(new DOMSource(domDoc), result); return sw.toString(); } catch (Exception ex) { throw new IOException(String.format("Error converting from doc to string %s", ex.getMessage())); } }
From source file:Main.java
public static void saveDocument(Document document, String path) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerFactoryConfigurationError, TransformerException, IOException { StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); DOMSource dom = new DOMSource(document); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 4); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(dom, sr);//from w w w . j a va 2 s .co m String string = sw.toString(); FileWriter fw = new FileWriter(new File(path)); fw.write(string); fw.close(); }
From source file:Main.java
public static String prettyFormat(String input, int indent, boolean isOmitXmlDeclaration) { try {/* w w w . j av a2 s . c o m*/ Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); if (isOmitXmlDeclaration) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { throw new RuntimeException(e); // simple exception handling, please review it } }
From source file:Main.java
public static void printXml(Document xml, Writer out) throws TransformerException, UnsupportedEncodingException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(xml), new StreamResult(out)); }
From source file:Main.java
/** * Convert XML Node to String/*from w w w . jav a2s. 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
/** * Creates a new document transformer/*from w ww . j a va2 s.com*/ * @return the transformer */ public static Transformer createTransformer() throws TransformerConfigurationException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); return transformer; }
From source file:Main.java
/** * Writes an XML document to an output stream. * * @param document/* w w w.ja v a 2 s.c o m*/ * @param outputStream * @throws TransformerException if the output failed */ public static void writeDocument(Document document, OutputStream outputStream, String encoding) throws TransformerException { Source source = new DOMSource(document); Result result = new StreamResult(outputStream); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty("indent", "yes"); // make the output easier to read, see Transformer.getOutputProperties transformer.setOutputProperty("encoding", encoding); transformer.transform(source, result); }