List of usage examples for javax.xml.transform TransformerFactory setAttribute
public abstract void setAttribute(String name, Object value);
From source file:Main.java
/** * /* w w w .j a va 2s . c o m*/ * @param xml * @param indent * @return pretty formatted xml */ public static String prettyFormat(String xml, int indent) { try { Source xmlInput = new StreamSource(new StringReader(xml)); 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 (Exception e) { throw new RuntimeException(e); // simple exception handling, please review it } }
From source file:Main.java
public static String toXML(Document document, boolean format) throws Exception { if (format) { removeWhitespaceNodes(document.getDocumentElement()); }//ww w. j a v a 2s . co m TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 2); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(out)); return out.toString(); }
From source file:Main.java
/** * Executes a transformation./*from w w w .ja v a2s .c om*/ * <br>The output encoding is set to UTF-8 * @param source the transformation source * @param result the transformation result * @param indent if true, the output indent key is set to "yes" * @throws TransformerException if an exception occurs */ public static void transform(javax.xml.transform.Source source, javax.xml.transform.Result result, boolean indent) throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); factory.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true); //factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, DEFAULT_ENCODING); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); if (indent) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); } transformer.transform(source, result); }
From source file:Main.java
public static void saveDomSource(final DOMSource source, final File target, final File xslt) throws TransformerException, IOException { TransformerFactory transFact = TransformerFactory.newInstance(); transFact.setAttribute("indent-number", 2); Transformer trans;//w w w . j av a2 s .co m if (xslt == null) { trans = transFact.newTransformer(); } else { trans = transFact.newTransformer(new StreamSource(xslt)); } trans.setOutputProperty(OutputKeys.INDENT, "yes"); FileOutputStream fos = new FileOutputStream(target); trans.transform(source, new StreamResult(new OutputStreamWriter(fos, "UTF-8"))); fos.close(); }
From source file:Main.java
public static String printXMLNode(Node node) { try {//from www . j a v a2 s . co m TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", 2); Transformer transformer = tf.newTransformer(); String omitDeclaration = node instanceof Document || node == node.getOwnerDocument().getDocumentElement() ? "no" : "yes"; transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(writer)); String output = writer.getBuffer().toString(); output = removeEmptyLines(output); // .replaceAll("\n|\r", ""); return output; } catch (TransformerException te) { throw new RuntimeException(te); } }
From source file:Main.java
/** * @see //http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java *///from w w w.j a v a 2 s .c om public static String toXML(Document document, boolean format) throws TransformerException { if (format) { removeWhitespaceNodes(document.getDocumentElement()); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 2); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(out)); return out.toString(); }
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 ww w. ja v a2 s.c o 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 ww . j a v a 2 s . co 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
/** * Saves a DOM to a human-readable XML file (4-space indentation, UTF-8). * <p>//w w w.j a va2 s . co m * Contains workaround for various JVM bugs. * * @param document * The DOM * @param file * The target XML file * @throws TransformerFactoryConfigurationError * In case of an XML transformation factory configuration error * @throws TransformerException * In case of an XML transformation error * @throws IOException * In case of an I/O error */ public static void saveHumanReadable(Document document, File file) throws TransformerFactoryConfigurationError, TransformerException, IOException { // Various indentation and UTF8 encoding bugs are worked around here TransformerFactory factory = TransformerFactory.newInstance(); factory.setAttribute("indent-number", new Integer(4)); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), "UTF8"); transformer.transform(new DOMSource(document), new StreamResult(writer)); writer.close(); }
From source file:Main.java
/** * Formats a xml string//from w ww . j a v a2s . c o m * @param input * @param indent * @return */ public static String prettyFormatXml(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 (Exception e) { e.printStackTrace(); } return input; }