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
public static void writeDocument(OutputStream iStream, Element iElement) throws TransformerException { TransformerFactory lTransformFactory = TransformerFactory.newInstance(); Transformer lTransformer = lTransformFactory.newTransformer(); DOMSource lSource = new DOMSource(iElement); StreamResult lStreamResult = new StreamResult(iStream); lTransformer.transform(lSource, lStreamResult); }
From source file:Main.java
/** * Converts a DOM Tree to a String.// w ww . j a v a 2s. co m * * @param xml The DOM document to convert. * @param file The file to save to. * * @return true if there were no errors saving to disk. */ public static boolean xmlToFile(Document xml, File file) { try { TransformerFactory f = TransformerFactory.newInstance(); OutputStream writer = new FileOutputStream(file); Transformer t = f.newTransformer(); DOMSource src = new DOMSource(xml); StreamResult result = new StreamResult(writer); t.transform(src, result); writer.close(); return true; } catch (Exception e) { return false; } }
From source file:Main.java
public static String parse(Document document) { try {/*from w ww . j a va2 s.c o m*/ TransformerFactory dbf = TransformerFactory.newInstance(); Transformer t = dbf.newTransformer(); t.setOutputProperty("encoding", "utf-8"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); t.transform(new DOMSource(document), new StreamResult(bos)); return bos.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static Node bytesToXml(final byte[] body) throws TransformerException { final Transformer transformer = FACTORY.newTransformer(); final Source source = new StreamSource(new ByteArrayInputStream(body)); final DOMResult result = new DOMResult(); transformer.transform(source, result); return result.getNode(); }
From source file:Main.java
/** * Convert a W3C Document to a String.//from w w w . j a v a 2 s .c om * * 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
/** * Stores an XMl document into a file./* ww w. j a v a2 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 getStringFromXmlDoc(org.w3c.dom.Node node) throws Exception { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.getBuffer().toString().replaceAll("\n|\r", ""); }
From source file:Main.java
public static void doc2stream(Document doc, StreamResult result) throws TransformerConfigurationException, TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(doc); transformer.transform(source, result); }
From source file:Main.java
public static final void prettyPrint(Document xml) throws Exception { Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); tf.transform(new DOMSource(xml), new StreamResult(out)); System.out.println(out.toString()); }
From source file:Main.java
public static String toXML(Document document, boolean format) throws Exception { if (format) { removeWhitespaceNodes(document.getDocumentElement()); }//from ww w . j av a2 s. co m TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 2); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(out)); return out.toString(); }