List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
/** * Convert xml to byte array.// w ww. j a v a 2 s . com * * @param pDocument * the p document * @param encoding * the encoding * @return the byte[] * @throws Exception * the exception */ public static byte[] convertXML2ByteArray(Node pDocument, String encoding) throws Exception { TransformerFactory transformerFactory = null; Transformer transformer = null; DOMSource domSource = null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); StreamResult result = null; try { transformerFactory = TransformerFactory.newInstance(); if (transformerFactory == null) { throw new Exception("TransformerFactory error"); } transformer = transformerFactory.newTransformer(); if (transformer == null) { throw new Exception("Transformer error"); } if (encoding != null) { transformer.setOutputProperty(OutputKeys.ENCODING, encoding); } else { transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); } domSource = new DOMSource(pDocument); result = new StreamResult(outputStream); transformer.transform(domSource, result); return outputStream.toByteArray(); } catch (TransformerFactoryConfigurationError e) { throw new Exception("TransformerFactoryConfigurationError", e); } catch (TransformerConfigurationException e) { throw new Exception("TransformerConfigurationException", e); } catch (TransformerException e) { throw new Exception("TransformerException", e); } finally { try { if (outputStream != null) { outputStream.close(); } } catch (IOException ex) { throw new Exception("IO error", ex); } } }
From source file:Main.java
public static String getDocumentAsString(Node node, boolean prettyXml) throws TransformerException, UnsupportedEncodingException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); if (prettyXml) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); }/*from w w w. j a v a2 s.c o m*/ transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); transformer.transform(new DOMSource(node), new StreamResult(baos)); return baos.toString("UTF-8"); }
From source file:Main.java
private static synchronized void initFactories() { // initialize static factories if (docBuilderFactory == null) docBuilderFactory = DocumentBuilderFactory.newInstance(); if (transformerFactory == null) transformerFactory = TransformerFactory.newInstance(); }
From source file:Main.java
/** * Converts an XML document to a formatted XML string. * //from w w w. j a v a 2 s .c o m * @param doc The document to format. * @return Formatted XML document. */ public static String toString(Document doc) { if (doc == null) { return ""; } 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.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(domSource, result); return writer.toString(); } catch (Exception e) { return e.toString(); } }
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
/** * /*from w w w . j a va 2 s . c o m*/ * @param header Just a title for the stanza for readability. Single word no spaces since * it is inserted as the root element in the output. * @param xml The string to pretty print */ static public void prettyPrint(String header, String xml) { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3"); if (header != null) { xml = "\n<" + header + ">" + xml + "</" + header + '>'; } transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(System.out)); } catch (Exception e) { System.out.println("Something wrong with xml in \n---------------\n" + xml + "\n---------------"); e.printStackTrace(); } }
From source file:Main.java
/** * Save content of DOM to a file./* ww w .ja v a 2 s . co m*/ * @param doc Document * @param filePath String * @throws Exception */ public static void saveDomToFile(Document doc, String filePath) throws Exception { // Write the DOM to file. Source source = new DOMSource(doc); // Prepare the output file File file = new File(filePath); Result result = new StreamResult(file); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); }
From source file:Main.java
/** Write a DOM document to an OutputStream */ public static void writeXmlDocumentToStream(Document document, OutputStream stream) throws Exception { Source source = new DOMSource(document); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); StreamResult result = new StreamResult(stream); transformer.transform(source, result); stream.flush();/* ww w .j a v a 2 s . c o m*/ }
From source file:Main.java
public static void buildXml(Node node, Writer writer) throws TransformerFactoryConfigurationError, TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(new DOMSource(node), new StreamResult(writer)); }
From source file:Main.java
public static void writeXmlFile(Document doc, File file) throws TransformerConfigurationException, TransformerException { Source source = new DOMSource(doc); Result result = new StreamResult(file); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(source, result); }