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
/** * Serialize XML from a {@link Document} to an OutputStream. * @param doc/*ww w. ja v a2s .c o m*/ * @param out */ public static void serialiseXml(Document doc, Writer out) { DOMSource domSource = new DOMSource(doc.getDocumentElement()); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = null; try { serializer = tf.newTransformer(); } catch (TransformerConfigurationException e) { } serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "users.dtd"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); try { serializer.transform(domSource, streamResult); } catch (TransformerException e) { } }
From source file:Main.java
public static String elementToString(Element ele) { try {//from www . j a va 2 s. com StringWriter sw = new StringWriter(); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(ele); StreamResult result = new StreamResult(sw); transformer.setOutputProperty("indent", "yes"); transformer.transform(source, result); return sw.toString(); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static void getStringFromXML(Node node, String dtdFilename, Writer outputWriter) throws TransformerException { File dtdFile = null;/*w w w . j a v a2s. com*/ String workingPath = null; if (dtdFilename != null) { dtdFile = new File(dtdFilename); workingPath = System.setProperty("user.dir", dtdFile.getAbsoluteFile().getParent()); } try { if (node instanceof Document) node = ((Document) node).getDocumentElement(); TransformerFactory tranFact = TransformerFactory.newInstance(); Transformer tf = tranFact.newTransformer(); if (dtdFile != null) { tf.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dtdFile.getName()); tf.setOutputProperty(OutputKeys.INDENT, "yes"); } Source src = new DOMSource(node); Result dest = new StreamResult(outputWriter); tf.transform(src, dest); } finally { if (workingPath != null) System.setProperty("user.dir", workingPath); } }
From source file:com.github.jjYBdx4IL.utils.fma.FMAConfig.java
public static String formatXml(String xml) { try {// www. j a va2s .c om Transformer serializer = SAXTransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "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()); } catch (IllegalArgumentException | TransformerException e) { LOG.error("", e); return xml; } }
From source file:Main.java
/** This method dumps out a dom document to an output stream. * Header information is turned off.// ww w .j a v a 2s. co m * * @param doc Dom Document * @param os existing outputstream you wish to write the document to. */ public static void DOMtoOutputStream(Document doc, OutputStream os) { try { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource src = new DOMSource(doc); StreamResult result = new StreamResult(os); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(src, result); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
private static void writeTo(Result result, Document document) throws TransformerConfigurationException, TransformerException, FileNotFoundException, UnsupportedEncodingException { // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setAttribute("indent-number", new Integer(4)); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(document); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // NOI18N transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); // NOI18N transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); // NOI18N // indent the output to make it more legible... try {/*from w w w . j a va2s .c o m*/ transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N } catch (IllegalArgumentException e) { // the JAXP implementation doesn't support indentation, no big deal //e.printStackTrace(); } transformer.transform(source, result); }
From source file:Main.java
public static String XMLDocumentToString(Document _doc) { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans; String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; try {/*from ww w.ja v a 2 s .co m*/ trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(_doc); trans.transform(source, result); xmlString += sw.toString(); } catch (TransformerConfigurationException e) { System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage()); return null; } catch (TransformerException e) { System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage()); return null; } return xmlString; }
From source file:Main.java
/** * Prints a Node tree recursively./* w w w.j a v a 2 s. c om*/ * @param node A DOM tree Node * @param encoding character encoding * @return An xml String representation of the DOM tree. */ public static String print(Node node, String encoding) { if (node == null) { return null; } try { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty("omit-xml-declaration", "yes"); transformer.setOutputProperty("encoding", encoding); DOMSource source = new DOMSource(node); ByteArrayOutputStream os = new ByteArrayOutputStream(2000); StreamResult result = new StreamResult(os); transformer.transform(source, result); return os.toString(encoding); } catch (Exception e) { return null; } }
From source file:Main.java
public static void writeXml(Document doc, File output) throws TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(output); 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 buildXmlFile(Document doc, File file) { TransformerFactory tfactory = TransformerFactory.newInstance(); try {/*from w ww.ja v a2 s . c om*/ Transformer transformer = tfactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(file); transformer.setOutputProperty("encoding", "gb2312"); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } }