List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException { 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(OutputKeys.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"))); }
From source file:Main.java
public static String convertDocumentToString(Document doc) throws TransformerConfigurationException, TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); // Unquote below to remove XML declaration. // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString(); return output; }
From source file:Main.java
public static void dumpElement(Logger log, Element elem) { try {//from w w w . ja v a 2 s . c o m TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(elem), new StreamResult(buffer)); log.debug(buffer.toString()); } catch (TransformerFactoryConfigurationError | IllegalArgumentException | TransformerException ex) { log.error("Failed to dump element", ex); } }
From source file:Main.java
/** * potentially unsafe XML transformation. * @param source The XML input to transform. * @param out The Result of transforming the <code>source</code>. */// w w w. j ava 2s . c o m private static void _transform(Source source, Result out) throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); // this allows us to use UTF-8 for storing data, // plus it checks any well-formedness issue in the submitted data. Transformer t = factory.newTransformer(); t.transform(source, out); }
From source file:Main.java
public static void writeXml(Document document, File outxml) { try {/* w w w.j a v a 2 s .com*/ TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer; serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource domSource = new DOMSource(document); StreamResult streamResult = new StreamResult(outxml); serializer.transform(domSource, streamResult); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String documentToXmlString(Document document) { DOMSource domSource = new DOMSource(document); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer;//from w w w. ja va 2s . c o m try { transformer = tf.newTransformer(); transformer.transform(domSource, result); } catch (TransformerException e) { throw new RuntimeException(e.getMessage()); } return writer.toString(); }
From source file:Main.java
public static void writeDocumentToFile(Document d, File f) throws TransformerException, FileNotFoundException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(d); StreamResult result = new StreamResult(new FileOutputStream(f)); transformer.transform(source, result); }
From source file:Main.java
static public void toXml(Document xmldoc, OutputStream os) throws TransformerException { StreamResult out = new StreamResult(os); DOMSource domSource = new DOMSource(xmldoc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(domSource, out); }
From source file:Main.java
public static void saveXml(Document doc, String filename) { try {/* w ww . j a v a 2s .c o m*/ TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(doc); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); PrintWriter pw = new PrintWriter(new FileOutputStream(filename)); StreamResult result = new StreamResult(pw); transformer.transform(source, result); } catch (TransformerException tfe) { tfe.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
/** * Converts an XML {@link org.w3c.dom.Document} to a string. * * @param doc the XML document//from w w w.j a v a2s.c o m * @return the string representation of the XML document */ public static String documentToString(Document doc) { try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.getBuffer().toString(); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(System.err); } return ""; }