List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
public static String getXMLString(Node doc) throws TransformerException, TransformerConfigurationException { TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); StringWriter sw = new StringWriter(); Source src = new DOMSource(doc); Result dest = new StreamResult(sw); aTransformer.transform(src, dest);//from ww w .j a v a 2 s. c o m return sw.toString(); }
From source file:Main.java
public static void printDocument(final Document doc) { try {/*from w w w.ja va 2 s . c om*/ final TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); 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", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(System.out, "UTF-8"))); } catch (final TransformerConfigurationException e) { e.printStackTrace(); } catch (final UnsupportedEncodingException e) { e.printStackTrace(); } catch (final TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
/** * // ww w . j a v a 2 s. c om * @param doc * @return */ public static String document2String(Document doc) { 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.ENCODING, "utf-8"); transformer.transform(domSource, result); return writer.toString(); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:Main.java
/** * Persists the XML to the file system./*from www . j a v a2s .co m*/ * * @param src the source XML. * @param file the file to persist to. * @throws TransformerException */ public static void persist(Source src, File file) throws TransformerException { final TransformerFactory tranFactory = TransformerFactory.newInstance(); final Transformer aTransformer = tranFactory.newTransformer(); final Result dest = new StreamResult(file); aTransformer.transform(src, dest); }
From source file:Main.java
public static final String createStringFromDOMNode(Node node, boolean omitDeclaration) { assert node != null; StringWriter out = null;//from ww w.j a va 2 s. co m try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); node.normalize(); Source source = new DOMSource(node); out = new StringWriter(); Result resultStream = new StreamResult(out); if (omitDeclaration) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } else { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); } transformer.transform(source, resultStream); } catch (Exception e) { } if (out != null) { return out.toString(); } return null; }
From source file:Main.java
/** * Transform xml Document to String/* w w w . ja v a2 s . co m*/ * @param doc Xml Document * @return String representation */ public static String getContent(final Document doc) { try { final StringWriter sw = new StringWriter(); final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); } catch (Exception ex) { throw new RuntimeException("Error converting to String", ex); } }
From source file:Main.java
/** * Saves the document in the given file//w w w .j a v a 2 s .c o m * @param modelDoc * @param fileName * @throws Exception */ public static void saveDocument(Document doc, File file) throws Exception { if (doc == null || file == null) return; TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(file); transformer.transform(source, result); }
From source file:Main.java
/** * Method to transform an XML document into a pretty-formatted string. * @param xml/* w w w .j a v a 2 s .c o m*/ * @return * @throws Exception */ public static final String xmlToString(Document xml) throws Exception { Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); tf.transform(new DOMSource(xml), new StreamResult(out)); return out.toString(); }
From source file:Main.java
/** * Transforms an XML to a String/* w w w . j a v a 2 s .com*/ * @param node XML node * @return String represenation of XML */ public static String printXML(Node node) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer; try { serializer = tfactory.newTransformer(); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); StringWriter output = new StringWriter(); serializer.transform(new DOMSource(node), new StreamResult(output)); return output.toString(); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void transform(Document src, Writer dst, String xsl) throws TransformerException { DOMSource dsrc = new DOMSource(src); StreamResult res = new StreamResult(dst); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(new StreamSource(xsl)); t.setOutputProperty(OutputKeys.METHOD, "xml"); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.transform(dsrc, res);//from w w w . j av a2 s. c o m }