List of usage examples for javax.xml.transform OutputKeys INDENT
String INDENT
To view the source code for javax.xml.transform OutputKeys INDENT.
Click Source Link
From source file:Main.java
public static void writeXMLDocument(Document doc, StreamResult out) throws TransformerConfigurationException, TransformerException { DOMSource domSource = new DOMSource(doc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(domSource, out); }
From source file:Main.java
/** * Node to string./* w ww. jav a 2s. c om*/ * * @param node the node * @return the string */ public static String nodeToString(final Node node) { final StringWriter sw = new StringWriter(); try { final Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (final TransformerException te) { System.out.println("nodeToString Transformer Exception"); } return sw.toString(); }
From source file:Utils.java
public static void writeXml(Node n, OutputStream os) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); // identity//ww w. j av a2s .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 Transformer newTransformer(int indent) throws TransformerConfigurationException { TransformerFactory factory = newTransformerFactory(); factory.setAttribute("indent-number", Integer.valueOf(indent)); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); return transformer; }
From source file:Main.java
/** * Writes out XML./*from w ww . j a v a2 s .c om*/ * * @param doc document * @param file target file * @throws TransformerException on transformer error */ public static void writeXml(Document doc, File file) throws TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(file); transformer.transform(source, result); }
From source file:Main.java
/** * Helper to turn on indentation for a {@link Transformer} that works correctly for * both Saxon and Xalan./*from w w w . j a v a 2s .c o m*/ * * @param transformer {@link Transformer} to configure * @param indent required indentation, where 0 or more provides indentation and negative * numbers turns indentation off. */ public static void setIndentation(final Transformer transformer, final int indent) { if (indent >= 0) { final String indentString = String.valueOf(indent); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); /* Set custom properties for both Saxon and Xalan at once. * This appears safe to do without having to check the underlying processor. */ transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", indentString); transformer.setOutputProperty("{http://saxon.sf.net/}indent-spaces", indentString); } else { transformer.setOutputProperty(OutputKeys.INDENT, "no"); } }
From source file:Main.java
public static String toString(Document doc, String encoding, boolean indent) throws TransformerFactoryConfigurationError, TransformerException { Source source = new DOMSource(doc); StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, encoding); if (indent) { xformer.setOutputProperty(OutputKeys.INDENT, "yes"); }//from w w w . jav a2s. co m xformer.transform(source, result); return sw.getBuffer().toString(); }
From source file:Main.java
/** * Writes XML Document into an xml file. * /*ww w. j ava2s . c om*/ * @param fileName the target file with the full path * @param document the source document * @return boolean true if the file saved * @throws Exception */ public static boolean writeXmlFile(String fileName, Document document) throws Exception { // creating and writing to xml file File file = new File(fileName); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); // to prevent XML External Entities attack Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(new DOMSource(document), new StreamResult(file)); return true; }
From source file:Main.java
/** * Method to get formatted Xml string from Xml source * * @param payload Source/* w w w .java 2 s. c o m*/ * @return a formatted Xml string */ public static String getXmlStringFromSource(Source payload) { String result = null; StreamResult strResult = new StreamResult(new StringWriter()); if (payload != null) { try { TransformerFactory factory = TransformerFactory.newInstance(); //factory.setAttribute("indent-number", new Integer(2)); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1"); transformer.transform(payload, strResult); } catch (TransformerException e) { e.printStackTrace(); } result = strResult.getWriter().toString(); } return result; }
From source file:Main.java
public static String formatXMLStr(String xml) { String output = null;/*from w w w . jav a2s. co m*/ try { Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new ByteArrayInputStream(xml.getBytes())).getDocumentElement(); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer)); // output = writer.getBuffer().toString().replaceAll("\n|\r", ""); output = writer.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return output; }