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 writeXML(Document doc, String filepath) { try {/*from w w w . j ava 2 s . c om*/ 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 getStringFromXmlDoc(org.w3c.dom.Node node) throws Exception { TransformerFactory tf = TransformerFactory .newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", null); 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 encodeAsXml(Node dom, OutputStream os) throws Throwable { try {/* www .j a va 2 s .c o m*/ DOMSource source = new DOMSource(dom); StreamResult result = new StreamResult(os); Transformer transformer; transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(source, result); } catch (TransformerConfigurationException tce) { Throwable x = tce; if (tce.getException() != null) x = tce.getException(); throw x; } catch (TransformerException te) { Throwable x = te; if (te.getException() != null) x = te.getException(); throw x; } }
From source file:Main.java
public static byte[] dumpToByteArray(Document document) throws TransformerConfigurationException, TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); StreamResult result = new StreamResult(bos); transformer.transform(new DOMSource(document), result); byte[] array = bos.toByteArray(); return array; }
From source file:Main.java
/** * Stores document using specified writer object. This method does not close * writer object.// w ww. java 2 s . c o m * @param document XML document * @param transformer Transformer object to store document * @param writer Writer object where this method stores XML document * @throws TransformerException If cannot store */ public static void storeDocument(Document document, Transformer transformer, Writer writer) throws TransformerException { StreamResult result = new StreamResult(writer); transformer.transform(new DOMSource(document), result); }
From source file:Main.java
public static synchronized void createStorage() throws ParserConfigurationException, TransformerException { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement(MESSAGES); doc.appendChild(rootElement);/* www . j av a2 s . c om*/ Transformer transformer = getTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(STORAGE_LOCATION)); transformer.transform(source, result); }
From source file:Main.java
public static String transformDOMToString(DOMSource source) { try {//from w w w . j a va 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
/** * Transform.//from w w w .j a va 2s.co m * * @param xmlSource * the xml source * @param xsltSource * the xslt source * @param outputStream * the output stream * @throws TransformerFactoryConfigurationError * the transformer factory configuration error * @throws TransformerConfigurationException * the transformer configuration exception * @throws TransformerException * the transformer exception */ public static void transform(final Source xmlSource, final Source xsltSource, final OutputStream outputStream) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException { final Transformer transformer = getTransformer(xsltSource); transformer.transform(xmlSource, new StreamResult(outputStream)); }
From source file:Main.java
public static String serializeDocument(Document document) throws Exception { // Serialize XML document to String. StringWriter writer = new StringWriter(); StreamResult streamResult = new StreamResult(writer); DOMSource domSource = new DOMSource(document); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); return writer.toString(); }
From source file:Main.java
/** * @see //http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java *//*from ww w .j av a 2 s .co m*/ public static String toXML(Document document, boolean format) throws TransformerException { if (format) { removeWhitespaceNodes(document.getDocumentElement()); } 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(); }