List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
/** * Saves a given XML document to the given output stream. *///w w w . j a v a 2 s. c om public static void writeXML(Document document, OutputStream os) throws IOException { DOMSource src = new DOMSource(document); StreamResult res = new StreamResult(os); TransformerFactory tf = TransformerFactory.newInstance(); try { Transformer t = tf.newTransformer(); t.transform(src, res); } catch (TransformerException e) { throw new IOException(e.getMessage()); } }
From source file:Main.java
/** * Convert XML Node to String//ww w.ja v a 2 s. c om * * @param node * the node to convert * @return the String equivalent of the node * @throws TransformerException */ public static String nodeToString(final Node node) throws TransformerException { final Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); final Writer out = new StringWriter(); tf.transform(new DOMSource(node), new StreamResult(out)); return out.toString(); }
From source file:Main.java
public static void saveDocument(Document doc, File file) throws TransformerConfigurationException, TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(file); transformer.transform(source, result); }
From source file:Main.java
public static void print(Node node, OutputStream out) throws UnsupportedEncodingException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(new DOMSource(node), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }
From source file:Main.java
public static void createEmptyXML222(String xmlFile) { try {/* www . j a v a 2 s. co m*/ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); FileOutputStream fo = new FileOutputStream(xmlFile); StreamResult result = new StreamResult(fo); transformer.transform(source, result); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
private static boolean writeTo(Document doc, String fileName) throws Exception { boolean isOver = false; DOMSource doms = new DOMSource(doc); File f = new File(fileName); StreamResult sr = new StreamResult(f); try {//w w w.j a v a2 s . c o m TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties properties = t.getOutputProperties(); properties.setProperty(OutputKeys.ENCODING, "UTF-8"); t.setOutputProperties(properties); t.transform(doms, sr); isOver = true; } catch (TransformerConfigurationException tce) { tce.printStackTrace(); } catch (TransformerException te) { te.printStackTrace(); } return isOver; }
From source file:Main.java
/** * Formatte un XML/*from w w w.j av a 2s .c om*/ * */ 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
private static void writeDo(Document doc, Writer w, String encoding) throws TransformerFactoryConfigurationError, TransformerException { Source source = new DOMSource(doc); Result result = new StreamResult(w); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, encoding); xformer.transform(source, result);/*from www. j a va 2 s . c o m*/ }
From source file:Main.java
public static void writeXMLFile(Node node, String filename) throws IOException { try {/*from w ww. j a v a 2s .com*/ // Prepare the DOM document for writing DOMSource source = new DOMSource(node); // Prepare the output file StreamResult result = new StreamResult(filename); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); } catch (TransformerConfigurationException e) { throw new IOException(); } catch (TransformerException e) { throw new IOException(); } }
From source file:Main.java
public static String DocToString(Document doc) throws TransformerFactoryConfigurationError, TransformerException { if (doc == null) return new String(""); String xmlString = ""; Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //initialize StreamResult with File object to save to file StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); xmlString = result.getWriter().toString(); return xmlString; }