List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
public static String XMLToString(Document doc, Boolean singleLine) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = null; try {/*from w w w . ja v a 2 s .c o m*/ transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); try { transformer.transform(new DOMSource(doc), new StreamResult(writer)); } catch (TransformerException e) { e.printStackTrace(); } String string = writer.getBuffer().toString(); if (!singleLine) { return string; } return string.replaceAll("\n|\r", ""); }
From source file:Main.java
public static void write2Xml(Document document) throws FileNotFoundException, TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.transform(new DOMSource(document), new StreamResult(new FileOutputStream(filename))); }
From source file:Main.java
public static OutputStream streamDocument(Document doc) throws IOException, TransformerException { OutputStream out = null;// w w w . ja va2s . c o m TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty("omit-xml-declaration", "no"); transformer.setOutputProperty("method", "xml"); transformer.setOutputProperty("indent", "yes"); transformer.setOutputProperty("encoding", "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); return out; }
From source file:Main.java
public static String transformDOMToString(DOMSource source) { try {// w w w . j av a 2s .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
/** * @see //http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java *///from ww w. j av a2s . c om 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(); }
From source file:Main.java
public static String pretty(String xml) { try {/* ww w . j ava 2 s. c o m*/ Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StreamResult result = new StreamResult(new StringWriter()); Source source = new StreamSource(new StringReader(xml)); transformer.transform(source, result); return result.getWriter().toString().replace("\r\n", "\n").replace("\n\r", "\n").replace("\r", "\n"); } catch (TransformerFactoryConfigurationError | TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
private static void wirteXmlFile(Document doc, File file) throws TransformerException { // Write the content into xml file //logger.debug("Changes are done in xml file, Writing to the XML file"); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(file); transformer.transform(source, result); }
From source file:Main.java
private static void SaveCustomerFile(Document CustomerDoc, JFrame mainFrame) { try {/* w w w .ja v a 2s .c om*/ TransformerFactory Factory = TransformerFactory.newInstance(); Transformer Trans = Factory.newTransformer(); DOMSource source = new DOMSource(CustomerDoc); File f = new File("."); String FilePath = f.getAbsoluteFile().getParent() + "\\Customers.xml"; f = new File(FilePath); if (f.exists()) { f.delete(); } StreamResult Result = new StreamResult(f); Trans.setOutputProperty(OutputKeys.INDENT, "yes"); Trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "7"); Trans.transform(source, Result); } catch (TransformerException ex) { System.out.println(ex.getMessage()); JOptionPane.showMessageDialog(mainFrame, "There Was an Error Saving The File. Please Restart the Application."); System.exit(1); } }
From source file:Main.java
static String getXMLString(Document xmlDoc) throws Exception { StringWriter writer = null;//from w ww. j a v a 2s. c o m DOMSource source = new DOMSource(xmlDoc.getDocumentElement()); writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.transform(source, result); StringBuffer strBuf = writer.getBuffer(); return strBuf.toString(); }
From source file:Main.java
public static String toXML(Document document, boolean format) throws Exception { if (format) { removeWhitespaceNodes(document.getDocumentElement()); }// w w w .ja v a 2 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(); }