List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer() throws TransformerConfigurationException;
From source file:Main.java
public static String transformDOMToString(DOMSource source) { try {/*from ww w .j a v a 2 s.c o m*/ // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); ByteArrayOutputStream sos = new ByteArrayOutputStream(); StreamResult result = new StreamResult(sos); transformer.transform(source, result); return sos.toString(); } catch (TransformerException e) { throw new IllegalArgumentException(e); } }
From source file:Main.java
public static String transText(Node doc) throws Exception { TransformerFactory transfactory = TransformerFactory.newInstance(); Transformer transformer = transfactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Source source = new DOMSource(doc); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.transform(source, result); return sw.toString(); }
From source file:Main.java
public static String transform2String(Node node) throws TransformerException { final TransformerFactory transFactory = TransformerFactory.newInstance(); final Transformer transformer = transFactory.newTransformer(); final StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(buffer)); return buffer.toString(); }
From source file:Main.java
/** * Stores an XMl document into a file.//from w w w. j a v a 2 s .c o m * * @param doc xml document to be stored * @param location absolute path where to write down the document * @throws TransformerException If an unrecoverable error occurs during the * course of the transformation. */ public static void save(Document doc, String location) throws TransformerException { doc.normalize(); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult streamResult = new StreamResult(new File(location)); transformer.transform(source, streamResult); }
From source file:Main.java
public static String toXMLString(Document doc) throws TransformerException { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); // create string from xml tree StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result);/*from www. java 2 s . co m*/ String xmlString = sw.toString(); return xmlString; }
From source file:Main.java
/** * Process a w3c XML document into a Java String. * /*from w ww .j a v a2 s. c o m*/ * @param outputDoc * @return */ public static String printToString(Document doc) { try { doc.normalizeDocument(); DOMSource source = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(source, result); return writer.toString(); } catch (Exception e) { // e.printStackTrace(); return null; } }
From source file:Main.java
public static void saveXml(Document doc, String filename) { try {/*from w w w.j a v a 2 s.c o m*/ TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(doc); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); PrintWriter pw = new PrintWriter(new FileOutputStream(filename)); StreamResult result = new StreamResult(pw); transformer.transform(source, result); } catch (TransformerException tfe) { tfe.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
/** * Saves the document in the given file//from w w w .j a v a2 s . co m * @param modelDoc * @param fileName * @throws Exception */ public static void saveDocument(Document doc, File file) throws Exception { if (doc == null || file == null) return; TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(file); transformer.transform(source, result); }
From source file:Main.java
public static void writeXML(Document doc, String filepath) { try {/*from w ww . ja va2s . c o m*/ TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(filepath)); transformer.transform(source, result); } catch (TransformerException tfe) { tfe.printStackTrace(); } }
From source file:Main.java
public static String getDocumentAsXml(Document doc) { StringWriter sw = new java.io.StringWriter(); try {//from w ww . jav a 2 s . co m DOMSource domSource = new DOMSource(doc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult sr = new StreamResult(sw); transformer.transform(domSource, sr); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return sw.toString(); }