List of usage examples for javax.xml.transform Transformer transform
public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException;
Transform the XML Source
to a Result
.
From source file:Main.java
private static void transform(Document doc, Result result) throws Exception { try {/*from w ww .ja v a2s . co m*/ // Prepare the DOM document for writing Source source = new DOMSource(doc); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); throw e; } catch (TransformerException e) { e.printStackTrace(); throw e; } catch (Exception e) { e.printStackTrace(); throw e; } }
From source file:Main.java
public static void writeDocumentToFile(Document d, File f) throws TransformerException, FileNotFoundException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(d); StreamResult result = new StreamResult(new FileOutputStream(f)); transformer.transform(source, result); }
From source file:Main.java
public static String getStringFromNode(Node node) throws Exception { StringWriter writer = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(writer)); String xml = writer.toString(); return xml;/* www . j av a2s . com*/ }
From source file:Main.java
/** * Formatte un XML//w w w . j a va 2s.co m * */ public static String prettyFormat(String input, int indent) { try { Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Throwable e) { try { Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = 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", String.valueOf(indent)); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Throwable t) { return input; } } }
From source file:Main.java
public static void xmlToStream(Node n, OutputStream os) throws Exception { Source source = new DOMSource(n); // PrintWriter pr = new PrintWriter(new OutputStreamWriter(os,StandardCharsets.UTF_8),true); Result result = new StreamResult(os);//pr); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); }
From source file:Main.java
public static void xmlToStream(Node n, Writer writer) throws Exception { Source source = new DOMSource(n); // PrintWriter pr = new PrintWriter(new OutputStreamWriter(os,StandardCharsets.UTF_8),true); Result result = new StreamResult(writer);//pr); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); }
From source file:Main.java
/** * This method writes the provided XML Document to the provided OutputStream. * @param doc The XML Document to write to the stream. * @param out The OutputStream to write to. * @throws TransformerConfigurationException * @throws TransformerException/*w w w.j a va2 s.c o m*/ * * @author <a href='mailto:intere@gmail.com'>Eric Internicola</a> */ public static void writeDocument(Document doc, OutputStream out) throws TransformerConfigurationException, TransformerException { Transformer t = getNewTransformer(); DOMSource ds = new DOMSource(doc); StreamResult sr = new StreamResult(out); t.transform(ds, sr); }
From source file:Main.java
/** * Converts DOM document to a string/* www . j a va 2 s . c o m*/ * * @param XMLDocument the document you want to convert * @return String representation of you XML document */ public static String XMLToString(Document XMLDocument) { String xmlString = null; try { // Convert DOM to XML string StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); Transformer trans = TransformerFactory.newInstance().newTransformer(); //trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.transform(new DOMSource(XMLDocument), result); xmlString = sw.toString(); sw.close(); } catch (TransformerConfigurationException tcex) { } catch (TransformerException tex) { } catch (IOException iex) { } return xmlString; }
From source file:Main.java
public static void print(Document doc, Writer out) throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(out); transformer.transform(source, result); }
From source file:Main.java
public static void print(Element elm, OutputStream out) throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(elm); StreamResult result = new StreamResult(out); transformer.transform(source, result); }