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
/** * // w w w . j av a 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 toString(Node node) { DOMSource domSource = new DOMSource(node); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); try {//from w w w .ja va 2 s. c o m Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(domSource, result); return writer.toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Serialized a node to a string. XML declarations will be omitted. * // w w w . j a v a 2 s. c o m * @param node The node to be serialized. * * @return The serialized node. * * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static String nodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty("omit-xml-declaration", "yes"); StringWriter sw = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static String getXMLString(Element elm, boolean htmlMode) throws TransformerException { /* TODO: Certain HTML entities, like "⇒", currently seem impossible to represent, as the HTML mode will output them in a named form unsupported by the Swing HTML parser (e.g. "⇐"). *//* ww w .j a v a 2s . c o m*/ Transformer t; t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.METHOD, htmlMode ? "html" : "xml"); t.setOutputProperty(OutputKeys.INDENT, "no"); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, htmlMode ? "yes" : "no"); StringWriter ret = new StringWriter(); t.transform(new DOMSource(elm), new StreamResult(ret)); return ret.toString(); }
From source file:Main.java
public final static void setOutputProperty(final Transformer t, final String key, final boolean b) { t.setOutputProperty(key, toOutputPropertyValue(b)); }
From source file:Main.java
/** * Converts an XML document to a formatted XML string. * // w w w.ja v a2 s . c o m * @param doc The document to format. * @return Formatted XML document. */ public static String toString(Document doc) { if (doc == null) { return ""; } try { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(domSource, result); return writer.toString(); } catch (Exception e) { return e.toString(); } }
From source file:Main.java
/** * write out an XML file// w w w. ja v a 2 s .c o m * * @param doc * @param os * @throws TransformerException * @throws IOException */ public static void writeXML(Document doc, OutputStream os) throws TransformerException, IOException { // write out xml file TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //indent XML properly //formatXML(doc,doc.getDocumentElement()," "); //normalize document doc.getDocumentElement().normalize(); //write XML to file DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(os); transformer.transform(source, result); os.close(); }
From source file:Main.java
/** * Save an XML document into a file/*from w w w.jav a 2 s .c o m*/ * * @param document * the XML document to save * @param file * the file * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static void docToFile(final Document document, final File file) throws TransformerFactoryConfigurationError, TransformerException { final Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); tf.transform(new DOMSource(document), new StreamResult(file)); }
From source file:Main.java
public static String toXML(Document document, boolean format) throws Exception { if (format) { removeWhitespaceNodes(document.getDocumentElement()); }/*from www.j av a 2s .c om*/ 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 ww w . jav a 2 s.c o m*/ * <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); }