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 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; if (xslt == null) { trans = transFact.newTransformer(); } else {//from w w w . j a va 2s . com 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:ac.uk.diamond.sample.HttpClientTest.Utils.java
static String xmlToString(Element doc) { StringWriter sw = new StringWriter(); try {//from www . j a v a2 s . c om TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); } catch (TransformerException e) { LOG.error("Unable to print message contents: ", e); return "<ERROR: " + e.getMessage() + ">"; } }
From source file:Main.java
public static void write(Source input, Result output, boolean indent) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { boolean omitxmldeclaration = true; Transformer idTransform = TransformerFactory.newInstance().newTransformer(); if (omitxmldeclaration) { idTransform.setOutputProperty("omit-xml-declaration", "yes"); }/*from ww w .ja v a 2s .c o m*/ idTransform.setOutputProperty("encoding", "UTF-8"); if (indent) { idTransform.setOutputProperty("indent", "yes"); try { idTransform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); } catch (Exception e) { // } } idTransform.transform(input, output); }
From source file:com.hangum.tadpole.engine.sql.util.export.XMLExporter.java
/** * make content file/*from w w w . j a v a 2 s . c o m*/ * * @param tableName * @param rsDAO * @return * @throws Exception */ public static String makeContentFile(String tableName, QueryExecuteResultDTO rsDAO) throws Exception { String strTmpDir = PublicTadpoleDefine.TEMP_DIR + tableName + System.currentTimeMillis() + PublicTadpoleDefine.DIR_SEPARATOR; String strFile = tableName + ".xml"; String strFullPath = strTmpDir + strFile; final StringWriter stWriter = new StringWriter(); final List<Map<Integer, Object>> dataList = rsDAO.getDataList().getData(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); final Document doc = builder.newDocument(); final Element results = doc.createElement(tableName); doc.appendChild(results); Map<Integer, String> mapLabelName = rsDAO.getColumnLabelName(); for (int i = 0; i < dataList.size(); i++) { Map<Integer, Object> mapColumns = dataList.get(i); Element row = doc.createElement("Row"); results.appendChild(row); for (int j = 1; j < mapColumns.size(); j++) { String columnName = mapLabelName.get(j); String strValue = mapColumns.get(j) == null ? "" : "" + mapColumns.get(j); Element node = doc.createElement(columnName); node.appendChild(doc.createTextNode(strValue)); row.appendChild(node); } } DOMSource domSource = new DOMSource(doc); TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", 4); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");//"ISO-8859-1"); StreamResult sr = new StreamResult(stWriter); transformer.transform(domSource, sr); FileUtils.writeStringToFile(new File(strFullPath), stWriter.toString(), true); return strFullPath; }
From source file:Main.java
public static String XMLToString(Document doc, Boolean singleLine) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = null; try {//from ww w. jav a2 s . c om transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); try { transformer.transform(new DOMSource(doc), new StreamResult(writer)); } catch (TransformerException e) { e.printStackTrace(); } String string = writer.getBuffer().toString(); if (!singleLine) { return string; } return string.replaceAll("\n|\r", ""); }
From source file:net.sf.jabref.exporter.OpenDocumentSpreadsheetCreator.java
private static void exportOpenDocumentSpreadsheetXML(File tmpFile, BibDatabase database, List<BibEntry> entries) { OpenDocumentRepresentation od = new OpenDocumentRepresentation(database, entries); try (Writer ps = new OutputStreamWriter(new FileOutputStream(tmpFile), StandardCharsets.UTF_8)) { DOMSource source = new DOMSource(od.getDOMrepresentation()); StreamResult result = new StreamResult(ps); Transformer trans = TransformerFactory.newInstance().newTransformer(); trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.transform(source, result); } catch (Exception e) { throw new Error(e); }// w ww . ja va 2 s .c o m }
From source file:fr.aliasource.webmail.server.proxy.client.http.DOMUtils.java
public static void serialise(Document doc, OutputStream out) throws TransformerException { TransformerFactory fac = TransformerFactory.newInstance(); Transformer tf = fac.newTransformer(); tf.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ Source input = new DOMSource(doc.getDocumentElement()); Result output = new StreamResult(out); tf.transform(input, output);//from www .j a v a 2 s . c o m }
From source file:net.mumie.coursecreator.xml.GraphXMLizer.java
/** * Describe <code>doTransform</code> method here. * * @param document a <code>Node</code> value * @return a <code>ByteArrayOutputStream</code> value * @exception TransformerException if an error occurs * @exception UnsupportedEncodingException if an error occurs *//*from w ww . j a va 2 s. c om*/ private static ByteArrayOutputStream doTransform(Node document) throws TransformerException, UnsupportedEncodingException { try { // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "US-ASCII"); DOMSource source = new DOMSource(document); ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamWriter out = new OutputStreamWriter(baos, "ASCII"); StreamResult result = new StreamResult(out); transformer.transform(source, result); return baos; } catch (TransformerException te) { // Error generated by the parser CCController.dialogErrorOccured("GraphXMLizer: TransformerException", "GraphXMLizer: TransformerException: " + te, JOptionPane.ERROR_MESSAGE); throw te; } }
From source file:Main.java
/** * @param doc// ww w .j av a 2s .c o m * @return * @throws RuntimeException */ public static String prettyPrint(Document doc) throws RuntimeException { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer; StringWriter writer = new StringWriter(); try { serializer = tfactory.newTransformer(); // Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //$NON-NLS-1$ //$NON-NLS-2$ serializer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.toString(); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String getXMLStringFromDocument(Document doc) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = null; try {//from w w w.j ava2 s .c om transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); try { transformer.transform(new DOMSource(doc), new StreamResult(writer)); } catch (TransformerException e) { throw new RuntimeException("Error while transforming XML document to String : ", e); } String output = writer.getBuffer().toString(); return output; }