List of usage examples for javax.xml.transform OutputKeys METHOD
String METHOD
To view the source code for javax.xml.transform OutputKeys METHOD.
Click Source Link
From source file:Main.java
public static void saveReportToFile(Node node, String documentFileName) { // StringWriter writer = new StringWriter(); // StreamResult resultString = new StreamResult(writer); File output = new File(documentFileName); StreamResult resultFile = new StreamResult(output); DOMSource source = new DOMSource(node); try {/* w w w .j a v a 2 s . co m*/ Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.METHOD, "xml"); t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); t.transform(source, resultFile); // t.transform(source, resultString); } catch (TransformerException e) { e.printStackTrace(); } // return writer.toString(); }
From source file:Main.java
public static void format(Source source, boolean omitDeclaration, Result result) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); try {/*from w w w . java2 s.c o m*/ transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); } catch (IllegalArgumentException exc) { // not supported } if (omitDeclaration) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } transformer.transform(source, result); }
From source file:Main.java
public static void writeDocument(Document document, String location) { DOMSource source = new DOMSource(document); File file = new File(location); Result result = new StreamResult(file); Transformer xformer;/*from w w w . j a va2 s .c o m*/ try { xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); xformer.setOutputProperty(OutputKeys.METHOD, "xml"); xformer.transform(source, result); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:Main.java
public static String formatDOM(Node node) throws TransformerException { StringWriter writer = new StringWriter(); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer;// ww w .ja 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"); // encoding doesn't matter since we stick with strings transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.toString(); }
From source file:Main.java
/** * Method for static initializer/*from ww w .ja va 2s . com*/ */ private static Properties createDefaultPropertiesForXML(boolean omitXMLDeclaration) { final Properties format = new Properties(); format.put(OutputKeys.METHOD, "xml"); format.put(OutputKeys.OMIT_XML_DECLARATION, (omitXMLDeclaration ? "yes" : "no")); format.put(OutputKeys.INDENT, "yes"); return format; }
From source file:Main.java
public static void saveXml(Document modDoc, String path) throws TransformerException, IOException { Writer writer = null;// www . j av a 2s .c o m try { TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); Source src = new DOMSource(modDoc); writer = getFileWriter(path); Result dest = new StreamResult(writer); aTransformer.setOutputProperty(OutputKeys.INDENT, "yes"); aTransformer.setOutputProperty(OutputKeys.METHOD, "xml"); aTransformer.transform(src, dest); } catch (TransformerException e) { throw e; } finally { writer.close(); } }
From source file:Main.java
public static String generateXml() { DocumentBuilder documentBuilder = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {/*from w ww . j a va 2s . c o m*/ documentBuilder = docFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); DOMSource docSource = new DOMSource(document); StreamResult result = new StreamResult(); Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); result.setOutputStream(baos); transformer.transform(docSource, result); } catch (Exception e) { e.printStackTrace(); } return baos.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
public static boolean write(String filename, Document document, boolean addDocType) { try {/*from w w w . j a v a 2 s. co m*/ TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", new Integer(4)); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); if (addDocType) { transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://java.sun.com/dtd/facelet-taglib_1_0.dtd"); } transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(source, new StreamResult(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename))))); return true; } catch (Exception e) { return false; } }
From source file:Main.java
/** * ***************************************** * Convert XML document to string//from w w w.j a v a2 s. c om * ******************************************. * * @param xmlDocument the xml document * @return the string * @throws IOException Signals that an I/O exception has occurred. * @throws TransformerException the transformer exception */ public static String converXmlDocToString(Document xmlDocument) throws IOException, TransformerException { String xmlString = ""; 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.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(xmlDocument), new StreamResult(writer)); xmlString = writer.getBuffer().toString().replaceAll("\n|\r", ""); return xmlString; }