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 nodeToWriter(Node node, Writer writer) throws Exception { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); DOMSource source = new DOMSource(node); StreamResult result = new StreamResult(writer); transformer.transform(source, result); }
From source file:Main.java
/** * Create and returns a new Transformer. *///from w ww . ja v a 2s.co m public static Transformer createTransformer() { initFactories(); final Transformer result; try { result = transformerFactory.newTransformer(); } catch (TransformerConfigurationException e) { return null; } result.setOutputProperty(OutputKeys.METHOD, "xml"); result.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); result.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); result.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); result.setOutputProperty(OutputKeys.INDENT, "yes"); return result; }
From source file:Main.java
public static String xmltoString(final Document document) throws TransformerException { StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(new DOMSource(document.getDocumentElement()), streamResult); return stringWriter.toString(); }
From source file:Main.java
public static String nodeToString(Node node) throws Exception { if (node == null) { return null; }// w w w .ja v a 2s .co m TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); DOMSource source = new DOMSource(node); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.transform(source, result); return sw.toString(); }
From source file:Main.java
/** * Print the indented document.//from w w w.j a v a 2s. c om * * @param stream * @param document * * @throws UnsupportedEncodingException * @throws TransformerExceptions */ public static void printXMLFormat(Document document) throws UnsupportedEncodingException, TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 4); //$NON-NLS-1$ Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$ transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ StreamResult streamResult = new StreamResult(); //$NON-NLS-1$ DOMSource source = new DOMSource(document); transformer.transform(source, streamResult); }
From source file:Main.java
/** * To output a DOM as a stream.//from ww w. j a v a 2 s. com */ public static InputStream documentToPrettyInputStream(Node document) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Source xmlSource = new DOMSource(document); Result outputTarget = new StreamResult(outputStream); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(xmlSource, outputTarget); InputStream is = new ByteArrayInputStream(outputStream.toByteArray()); return is; }
From source file:Main.java
/** * To output a DOM as a stream./*from w w w . jav a 2 s . c o m*/ */ public static InputStream documentToPrettyInputStream(Document document) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Source xmlSource = new DOMSource(document); Result outputTarget = new StreamResult(outputStream); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(xmlSource, outputTarget); InputStream is = new ByteArrayInputStream(outputStream.toByteArray()); return is; }
From source file:Main.java
public static void transformDocument(Document doc, String xsl, String targetFile) { try {//from w w w .java 2s . c o m Source source = new DOMSource(doc); // Creation of the output file File file = new File(targetFile); Result result = new StreamResult(file); // configuration of the transformer TransformerFactory factoryT = TransformerFactory.newInstance(); StreamSource stylesource = new StreamSource(xsl); Transformer transformer; transformer = factoryT.newTransformer(stylesource); transformer.setOutputProperty(OutputKeys.METHOD, "text"); // Transformation transformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static void transformDocument(final Document doc, final String xsl, final String targetFile) { try {/*from w w w .j a va2s . c o m*/ final Source source = new DOMSource(doc); // Creation of the output file final File file = new File(targetFile); final Result result = new StreamResult(file); // configuration of the transformer final TransformerFactory factoryT = TransformerFactory.newInstance(); final StreamSource stylesource = new StreamSource(xsl); Transformer transformer; transformer = factoryT.newTransformer(stylesource); transformer.setOutputProperty(OutputKeys.METHOD, "text"); // Transformation transformer.transform(source, result); } catch (final TransformerConfigurationException e) { e.printStackTrace(); } catch (final TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static String toXMLString(final DOMSource value) { final StringWriter writer = new StringWriter(); try {//ww w. ja va2s . com final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(value, new StreamResult(writer)); } catch (final Throwable t) { //todo throw flash error throw new RuntimeException(t); } final String result = writer.toString(); return result.substring(0, result.length() - 1); }