List of usage examples for javax.xml.transform Transformer setOutputProperty
public abstract void setOutputProperty(String name, String value) throws IllegalArgumentException;
From source file:com.hangum.tadpole.engine.sql.util.QueryUtils.java
/** * query to xml/* w w w . j a v a 2s . c o m*/ * * @param userDB * @param strQuery * @param listParam */ @SuppressWarnings("deprecation") public static String selectToXML(final UserDBDAO userDB, final String strQuery, final List<Object> listParam) throws Exception { final StringWriter stWriter = new StringWriter(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); final Document doc = builder.newDocument(); final Element results = doc.createElement("Results"); doc.appendChild(results); SqlMapClient client = TadpoleSQLManager.getInstance(userDB); QueryRunner qr = new QueryRunner(client.getDataSource()); qr.query(strQuery, listParam.toArray(), new ResultSetHandler<Object>() { @Override public Object handle(ResultSet rs) throws SQLException { ResultSetMetaData metaData = rs.getMetaData(); while (rs.next()) { Element row = doc.createElement("Row"); results.appendChild(row); for (int i = 1; i <= metaData.getColumnCount(); i++) { String columnName = metaData.getColumnName(i); Object value = rs.getObject(i) == null ? "" : rs.getObject(i); Element node = doc.createElement(columnName); node.appendChild(doc.createTextNode(value.toString())); row.appendChild(node); } } return stWriter.toString(); } }); 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); return stWriter.toString(); }
From source file:hydrograph.ui.common.util.XMLUtil.java
/** * /* w w w . j ava 2s .com*/ * Format given XML string * * @param xmlString * @return String */ public static String formatXML(String xmlString) { try (Writer writer = new StringWriter()) { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(TRANSFORMER_INDENT_AMOUNT_KEY, INDENT_SPACE); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); //initialize StreamResult with File object to save to file StreamResult result = new StreamResult(writer); Document xmlDoc = convertStringToDocument(xmlString); if (xmlDoc == null) { return xmlString; } DOMSource source = new DOMSource(xmlDoc); transformer.transform(source, result); return result.getWriter().toString(); } catch (TransformerException e) { logger.debug("Unable to format XML string", e); } catch (IOException e) { logger.debug("Unable to format XML string", e); } return null; }
From source file:com.moviejukebox.tools.DOMHelper.java
/** * Write the Document out to a file using nice formatting * * @param doc The document to save//from w w w . ja va2 s .com * @param localFile The file to write to * @return */ public static boolean writeDocumentToFile(Document doc, File localFile) { try { Transformer trans = TransformerFactory.newInstance().newTransformer(); // Define the output properties trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); trans.setOutputProperty(OutputKeys.INDENT, YES); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); doc.setXmlStandalone(true); trans.transform(new DOMSource(doc), new StreamResult(localFile)); return true; } catch (IllegalArgumentException | DOMException | TransformerException error) { LOG.error("Error writing the document to {}", localFile); LOG.error("Message: {}", error.getMessage()); return false; } }
From source file:com.hangum.tadpole.engine.sql.util.QueryUtils.java
/** * execute DML/* ww w .ja v a2 s .c o m*/ * * @param userDB * @param strQuery * @param listParam * @param resultType * @throws Exception */ public static String executeDML(final UserDBDAO userDB, final String strQuery, final List<Object> listParam, final String resultType) throws Exception { SqlMapClient client = TadpoleSQLManager.getInstance(userDB); Object effectObject = runSQLOther(userDB, strQuery, listParam); String strReturn = ""; if (resultType.equals(RESULT_TYPE.CSV.name())) { final StringWriter stWriter = new StringWriter(); CSVWriter csvWriter = new CSVWriter(stWriter, ','); String[] arryString = new String[2]; arryString[0] = "effectrow"; arryString[1] = String.valueOf(effectObject); csvWriter.writeNext(arryString); strReturn = stWriter.toString(); } else if (resultType.equals(RESULT_TYPE.JSON.name())) { final JsonArray jsonArry = new JsonArray(); JsonObject jsonObj = new JsonObject(); jsonObj.addProperty("effectrow", String.valueOf(effectObject)); jsonArry.add(jsonObj); strReturn = JSONUtil.getPretty(jsonArry.toString()); } else {//if(resultType.equals(RESULT_TYPE.XML.name())) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); final Document doc = builder.newDocument(); final Element results = doc.createElement("Results"); doc.appendChild(results); Element row = doc.createElement("Row"); results.appendChild(row); Element node = doc.createElement("effectrow"); node.appendChild(doc.createTextNode(String.valueOf(effectObject))); 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"); final StringWriter stWriter = new StringWriter(); StreamResult sr = new StreamResult(stWriter); transformer.transform(domSource, sr); strReturn = stWriter.toString(); } return strReturn; }
From source file:de.bitzeche.video.transcoding.zencoder.ZencoderClient.java
protected static String XmltoString(Document document) throws TransformerException { StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(new DOMSource(document.getDocumentElement()), streamResult); return stringWriter.toString(); }
From source file:Main.java
/** * @param xml/*from www.j av a 2s. c om*/ */ public static String prettyPrintToString(String xml) { Source xmlInput = new StreamSource(new StringReader(xml)); StreamResult xmlOutput = new StreamResult(new StringWriter()); Transformer transformer = null; try { transformer = TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); try { transformer.transform(xmlInput, xmlOutput); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } String formattedxml = xmlOutput.getWriter().toString(); return formattedxml; }
From source file:Main.java
/** * @param xml//w ww . ja v a2 s .c o m */ public static void prettyPrint(String xml) { Source xmlInput = new StreamSource(new StringReader(xml)); StreamResult xmlOutput = new StreamResult(new StringWriter()); Transformer transformer = null; try { transformer = TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); try { transformer.transform(xmlInput, xmlOutput); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } String formattedxml = xmlOutput.getWriter().toString(); System.out.println(formattedxml); }
From source file:Main.java
private static Transformer getThreadedTransformer(final boolean omitXmlDeclaration, final boolean standalone, final Map<Thread, Transformer> threadMap, final String xslURL) throws TransformerConfigurationException { final Thread currentThread = Thread.currentThread(); Transformer transformer = null; if (threadMap != null) { transformer = threadMap.get(currentThread); }//from ww w . j av a 2s .c o m if (transformer == null) { if (xslURL == null) { transformer = tFactory.newTransformer(); // "never null" } else { transformer = tFactory.newTransformer(new StreamSource(xslURL)); } transformer.setOutputProperty(OutputKeys.INDENT, "yes"); if (threadMap != null) { threadMap.put(currentThread, transformer); } } transformer.setOutputProperty(OutputKeys.STANDALONE, standalone ? "yes" : "no"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no"); return transformer; }
From source file:Main.java
/** * Transform node./*from ww w .j a va 2 s .c o m*/ * * @param node * the node * @param encoding * the encoding * @param removeXmlHeader * the remove xml header * @param result * the result * @throws Exception * the exception */ private static void transformNode(Node node, String encoding, boolean removeXmlHeader, StreamResult result) throws Exception { TransformerFactory transformerFactory = null; Transformer transformer = null; DOMSource source = null; try { transformerFactory = TransformerFactory.newInstance(); if (transformerFactory == null) { throw new Exception("TransformerFactory error"); } transformer = transformerFactory.newTransformer(); if (transformer == null) { throw new Exception("Transformer error"); } transformer.setOutputProperty(OutputKeys.ENCODING, encoding); if (removeXmlHeader) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } else { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); } source = new DOMSource(node); transformer.transform(source, result); } catch (TransformerFactoryConfigurationError e) { throw new Exception("TransformerFactoryConfigurationError", e); } catch (TransformerConfigurationException e) { throw new Exception("TransformerConfigurationException", e); } catch (TransformerException e) { throw new Exception("TransformerException", e); } }