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 Transformer newTransformer(boolean indented) { String indentFlag = (indented) ? "yes" : "no"; try {//from w w w.jav a 2 s . co m TransformerFactory factory = TransformerFactory.newInstance(); factory.setAttribute("indent-number", DEFAULT_INDENT); Transformer transformer; transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.INDENT, indentFlag); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); return transformer; } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static Transformer getTransformer(boolean standalone, boolean indent, int indentNumber, boolean omitXmlDeclaration) throws TransformerException { TransformerFactory f = TransformerFactory.newInstance(); f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE); if (indent) { f.setAttribute("indent-number", indentNumber); }/*from w w w .j a v a 2 s .co m*/ Transformer t = f.newTransformer(); if (standalone) { t.setOutputProperty(OutputKeys.STANDALONE, "yes"); } if (indent) { t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty("{xml.apache.org/xslt}indent-amount", "" + indentNumber); } if (omitXmlDeclaration) { t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } return t; }
From source file:Main.java
public static void writeXml(Document document, File outxml) { try {//from ww w. j av a 2s . c o m TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer; serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource domSource = new DOMSource(document); StreamResult streamResult = new StreamResult(outxml); serializer.transform(domSource, streamResult); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void writeXml(Document document, OutputStream out) { try {// w w w . ja va2 s. c o m TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer; serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource domSource = new DOMSource(document); StreamResult streamResult = new StreamResult(out); serializer.transform(domSource, streamResult); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Write an XML document out to an output stream in UTF-8 * @param document - the document to write. * @param outputStream - the stream to which the XML will be written * @param indent - the indent level.//from ww w . j av a 2 s.c o m */ public static void writeXmlDocumentToStream(Document document, OutputStream outputStream, int indent) { try { Transformer transformer = transformerFactory.get().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, UTF8); if (indent > 0) { transformer.setOutputProperty(OutputKeys.INDENT, YES); transformer.setOutputProperty(INDENT_AMOUNT, Integer.toString(indent)); } Result result = new StreamResult(outputStream); Source source = new DOMSource(document); transformer.transform(source, result); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String document2String(Document document, String encode) throws TransformerException { String xml = null;/*w w w.j av a 2s.co m*/ TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); transformer.setOutputProperty("encoding", encode); transformer.setOutputProperty("indent", "yes"); StringWriter sw = new StringWriter(); transformer.transform(source, new StreamResult(sw)); xml = sw.toString(); return xml; }
From source file:Main.java
public static void nodeToString(Node node, StringBuffer buf) throws TransformerException { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); DOMSource dSource = new DOMSource(node); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); transformer.transform(dSource, sr);/*from www .j a v a2 s . c o m*/ StringWriter anotherSW = (StringWriter) sr.getWriter(); buf.append(anotherSW.getBuffer()); }
From source file:Main.java
public static String prettyFormat(String xml) { if (xml == null || xml.isEmpty() || !xml.contains("<")) { // System.out.println("Why?"+xml.startsWith("<", 0)); return xml; }//from w ww .ja va 2 s . c o m try { Transformer serializer = SAXTransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); //serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes()))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); serializer.transform(xmlSource, res); return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray()).replace("><", ">\n<"); } catch (Exception e) { System.out.println("prettyFormat: Error.." + e.getMessage()); //TODO log error return xml.replace("<", "\n<"); // return xml.replace("><", ">\n<"); } }
From source file:Main.java
/** * To output a DOM as a stream.//from w ww . j a v a 2s . 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
/** * Formatte un XML// ww w .j ava 2 s. c om * */ public static String prettyFormat(String input, int indent) { try { 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"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Throwable e) { try { Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = 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", String.valueOf(indent)); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Throwable t) { return input; } } }