List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer() throws TransformerConfigurationException;
From source file:Main.java
private static Transformer getTransformer() throws TransformerConfigurationException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); // Formatting XML properly transformer.setOutputProperty(OutputKeys.INDENT, "yes"); return transformer; }
From source file:Main.java
public static void printDOM(Node node, OutputStream out, String encoding) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer;// ww w . ja v a 2 s. co m transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(node), new StreamResult(out)); }
From source file:Main.java
public static boolean debug(Node paramNode) { try {// w ww . ja v a 2s . com TransformerFactory localTransformerFactory = TransformerFactory.newInstance(); Transformer localTransformer = localTransformerFactory.newTransformer(); DOMSource localDOMSource = new DOMSource(paramNode); StreamResult localStreamResult = new StreamResult(System.out); localTransformer.transform(localDOMSource, localStreamResult); return true; } catch (Exception localException) { localException.printStackTrace(System.out); } return false; }
From source file:Main.java
public static String toString(Element e) { try {/* w w w. j a va2 s . c om*/ TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer xform = tfactory.newTransformer(); Source src = new DOMSource(e); java.io.StringWriter writer = new StringWriter(); Result result = new javax.xml.transform.stream.StreamResult(writer); xform.transform(src, result); return writer.toString(); } catch (Exception ex) { return "Unable to convert to string: " + ex.toString(); } }
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
/** * Transform xml Document to String//w ww . j a v a 2s .c o m * @param doc Xml Document * @return String representation */ public static String getContent(final Document doc) { try { final StringWriter sw = new StringWriter(); final TransformerFactory tf = TransformerFactory.newInstance(); final 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.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); } catch (Exception ex) { throw new RuntimeException("Error converting to String", ex); } }
From source file:Main.java
/** * Saves Document instance to XML file to the specified filePath. * //from ww w . j av a 2s . com * @param xmlDoc - Document instance * @param destPath - dest path where the XML file is to be saved. * @throws Exception */ public static void saveDocument(Document xmlDoc, String destPath) throws Exception { Source xmlSource = new DOMSource(xmlDoc); OutputStream os = new FileOutputStream(destPath); Result result = new StreamResult(os); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); /* output to file */ transformer.transform(xmlSource, result); }
From source file:Main.java
/** * // w w w . j av a2 s. c o m * @param fileName : output file * @param document : xml document * @throws TransformerFactoryConfigurationError * @throws TransformerConfigurationException * @throws TransformerException */ private static void writeDocIntoFile(final String fileName, final Document document) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException { // update the XML file final TransformerFactory transformerFactory = TransformerFactory.newInstance(); final Transformer transformer = transformerFactory.newTransformer(); final DOMSource source = new DOMSource(document); final StreamResult result = new StreamResult(new File(fileName)); transformer.transform(source, result); }
From source file:Main.java
/** * Convert a W3C Document to a String./*from w w w. j a v a2s . c o m*/ * * Note: if you are working with a dom4j Document, you can use it's asXml() method. * * @param doc * org.w3c.dom.Document to be converted to a String. * @return String representing the XML document. * * @throws TransformerConfigurationException * If unable to get an instance of a Transformer * @throws TransformerException * If the attempt to transform the document fails. */ public static final StringBuffer docToString(final org.w3c.dom.Document doc) throws TransformerConfigurationException, TransformerException { StringBuffer sb = null; StringWriter writer = new StringWriter(); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); // can throw // TransformerConfigurationException Source docSrc = new DOMSource(doc); t.transform(docSrc, new StreamResult(writer)); // can throw // TransformerException sb = writer.getBuffer(); return sb; }
From source file:Main.java
/** * Converts an XML document to a formatted XML string. * /* w ww . ja va 2s .com*/ * @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(); } }