List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer() throws TransformerConfigurationException;
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 {// w ww .ja v a 2s . co 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
public static String getXMLString(Document dom, boolean bOmitDeclaration, String sEncoding) { String sOmit = (bOmitDeclaration ? "yes" : "no"); try {//from w ww . j ava 2s . c om TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); if (sEncoding != null) transformer.setOutputProperty(OutputKeys.ENCODING, sEncoding); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, sOmit); transformer.transform(new DOMSource(dom), new StreamResult(buffer)); return buffer.toString(); } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String convertDocumentToString(Document dom) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer;//from w ww .j av a 2 s . co m try { transformer = tf.newTransformer(); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(dom), new StreamResult(writer)); String output = writer.getBuffer().toString(); return output; } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Clones a document object.//from w w w .j av a 2s. com * * @param doc The document to be cloned. * @return The new document object that contains the same data as the original document. * @throws TransformerException Thrown if the document can't be */ public static Document cloneDocument(final Document doc) throws TransformerException { final Node rootNode = doc.getDocumentElement(); // Copy the doctype and xml version type data final TransformerFactory tfactory = TransformerFactory.newInstance(); final Transformer tx = tfactory.newTransformer(); final DOMSource source = new DOMSource(doc); final DOMResult result = new DOMResult(); tx.transform(source, result); // Copy the actual content into the new document final Document copy = (Document) result.getNode(); copy.removeChild(copy.getDocumentElement()); final Node copyRootNode = copy.importNode(rootNode, true); copy.appendChild(copyRootNode); return copy; }
From source file:Main.java
public static String documentToString(Document document) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer;/*from w w w .j ava2 s .co m*/ try { 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(document), new StreamResult(writer)); } catch (TransformerException e) { throw new RuntimeException(e); } return writer.getBuffer().toString().replaceAll("\n|\r", ""); }
From source file:Main.java
public static void printDocument(Document doc) { try {// w w w .ja v a2 s . c o m TransformerFactory tf = TransformerFactory.newInstance(); Transformer 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(new OutputStreamWriter(System.out, "UTF-8"))); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static String convertResultSetToXML(ResultSet rs) throws SQLException, ParserConfigurationException, TransformerException { ResultSetMetaData rsmd = rs.getMetaData(); int colCount = rsmd.getColumnCount(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element results = doc.createElement("Results"); doc.appendChild(results);/*from w ww . j a v a2 s.c o m*/ while (rs.next()) { Element row = doc.createElement("Row"); results.appendChild(row); for (int i = 1; i <= colCount; i++) { String columnName = rsmd.getColumnName(i); Object value = rs.getObject(i); if (value != null) { Element node = doc.createElement(columnName.replaceAll("\\(", "_").replaceAll("\\)", "_")); node.appendChild(doc.createTextNode(value.toString())); row.appendChild(node); } } } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString().replaceAll("\n|\r", ""); return output; }
From source file:Main.java
public static void saveXML(Document doc, OutputStream stream) { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer;/*from w w w. ja v a 2s . co m*/ try { transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(stream); transformer.transform(source, result); stream.flush(); stream.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void save(String paramString, Document paramDocument) throws Exception { DOMSource localDOMSource = new DOMSource(paramDocument); File localFile1 = new File(paramString); File localFile2 = localFile1.getParentFile(); localFile2.mkdirs();/* w w w .j a v a 2s. c o m*/ StreamResult localStreamResult = new StreamResult(localFile1); try { TransformerFactory localTransformerFactory = TransformerFactory.newInstance(); Transformer localTransformer = localTransformerFactory.newTransformer(); Properties localProperties = localTransformer.getOutputProperties(); localProperties.setProperty("encoding", "UTF-8"); localProperties.setProperty("indent", "yes"); localTransformer.setOutputProperties(localProperties); localTransformer.transform(localDOMSource, localStreamResult); } catch (TransformerConfigurationException localTransformerConfigurationException) { localTransformerConfigurationException.printStackTrace(); } catch (TransformerException localTransformerException) { localTransformerException.printStackTrace(); } }
From source file:Main.java
public static void saveXml(Document modDoc, String path) throws TransformerException, IOException { Writer writer = null;//w w w . j a v a 2 s. c o m try { TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); Source src = new DOMSource(modDoc); writer = getFileWriter(path); Result dest = new StreamResult(writer); aTransformer.setOutputProperty(OutputKeys.INDENT, "yes"); aTransformer.setOutputProperty(OutputKeys.METHOD, "xml"); aTransformer.transform(src, dest); } catch (TransformerException e) { throw e; } finally { writer.close(); } }