List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer() throws TransformerConfigurationException;
From source file:Main.java
/** * Create a new {@link Transformer}.// w w w . j a v a2 s . c om * * @throws TransformerConfigurationException */ public static Transformer createTransformer() throws TransformerConfigurationException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); return transformerFactory.newTransformer(); }
From source file:Main.java
public static void save(Document document, IFile file, IProgressMonitor monitor) throws TransformerException, CoreException, UnsupportedEncodingException { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.ENCODING, "utf-8"); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); DOMSource ds = new DOMSource(document); trans.transform(ds, sr);/*from www. j ava 2 s . c o m*/ String xmlString = sw.toString(); InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8")); if (file.exists()) { file.setContents(is, 0, monitor); } else { file.create(is, true, monitor); } }
From source file:Main.java
public static void printDom(Document dom, String filename) { try {/*from www . j a va 2 s.c o m*/ DOMSource source = new DOMSource(dom); StreamResult result; result = new StreamResult(new FileOutputStream(filename)); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.transform(source, result); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static String xmltoString(final 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
/** * Convert XML Document to a String./* www . java 2 s. c o m*/ * * @param node * @return */ public static String xmlToString(Node node) { try { Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * convert org.w3c.dom.Document to xml-String * //from www . j av a 2 s . c o m * @param document * @return String xml * @throws Exception */ private static String toString(Document document) throws Exception { Source source = new DOMSource(document); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); }
From source file:Main.java
/** * Updates existing file with Document object * //w w w .j av a 2s.co m * @param doc * xml document * @param file * xml file to update * @throws TransformerFactoryConfigurationError */ public static void updateXMLFile(Document doc, File file) throws TransformerFactoryConfigurationError { try { DOMSource source = new DOMSource(doc); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(file); transformer.transform(source, result); } catch (Exception e) { throw new RuntimeException("Exception while saving message to xml file", e); } }
From source file:Main.java
public static void formatDocument(Document document, Writer out, String encoding) throws TransformerException { document.normalizeDocument();// w w w .ja v a2s . c o m TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer idTransform = transFactory.newTransformer(); idTransform.setOutputProperty(OutputKeys.METHOD, "xml"); idTransform.setOutputProperty(OutputKeys.INDENT, "yes"); if (encoding != null) { idTransform.setOutputProperty(OutputKeys.ENCODING, encoding); } idTransform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source source = new DOMSource(document); Result result = new StreamResult(out); idTransform.transform(source, result); }
From source file:Main.java
/** * Get DOM as a string/*from w w w .jav a 2 s.c om*/ * @param doc * @return */ public static String getStringFromDoc(org.w3c.dom.Document doc) { if (doc == null) { System.out.println("XML document is null"); } 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.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://commons.omniupdate.com/dtd/standard.dtd"); transformer.transform(domSource, result); writer.flush(); return writer.toString(); } catch (TransformerException ex) { System.out.println("Transformer Exception"); // ex.printStackTrace(); return "Error in transformation"; } }
From source file:Main.java
public static String nodeToString(Node node) throws Exception { if (node == null) { return null; }//from www . j a va2 s. c om TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); DOMSource source = new DOMSource(node); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.transform(source, result); return sw.toString(); }