List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
/** * Save an XML document into a file// w ww. j a va2 s . c o m * * @param document * the XML document to save * @param file * the file * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static void docToFile(final Document document, final File file) throws TransformerFactoryConfigurationError, 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"); tf.transform(new DOMSource(document), new StreamResult(file)); }
From source file:Main.java
public static String XMLtoString(Document doc) throws Exception { try {/* ww w . jav a2 s. co m*/ TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; transformer = tf.newTransformer(); // below code to remove XML declaration // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.getBuffer().toString(); } catch (TransformerException te) { throw new Exception(te.getMessageAndLocation()); } }
From source file:Main.java
public static org.w3c.dom.Document writeToFile(String xmlContent, String path) { System.out.println("This is the path " + path); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder;//from ww w .j a va 2s . c o m try { builder = factory.newDocumentBuilder(); org.w3c.dom.Document doc = builder.parse(new InputSource(new StringReader(xmlContent))); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(path)); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
static void formatXMLFile(String file) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new InputStreamReader(new FileInputStream(file)))); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.METHOD, "xml"); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.setOutputProperty("b;http://xml.apache.org/xsltd;indent-amount", "4"); xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); Source source = new DOMSource(document); Result result = new StreamResult(new File(file)); xformer.transform(source, result);/* w w w.j av a 2 s . com*/ }
From source file:Main.java
public static void buildXmlFile(Document doc, File file) { TransformerFactory tfactory = TransformerFactory.newInstance(); try {//from ww w .j av a 2 s. co m Transformer transformer = tfactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(file); transformer.setOutputProperty("encoding", "gb2312"); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static byte[] doc2bytes(Document doc, boolean formated) { try {// w w w. j av a2 s.c o m Source source = new DOMSource(doc); ByteArrayOutputStream out = new ByteArrayOutputStream(); Result result = new StreamResult(out); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); if (formated) { // linefeed formatting transformer.setOutputProperty(OutputKeys.INDENT, "yes"); } else { // remove xml header transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } transformer.transform(source, result); return out.toByteArray(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Convert the document to an array of bytes. * /*from w ww . jav a 2s . c om*/ * @param doc * The XML document. * @param encoding * The encoding of the output data. * * @return The XML document as an array of bytes. * * @throws TransformerException * If there is an error transforming to text. */ public static byte[] asByteArray(Document doc, String encoding) throws TransformerException { final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); final StringWriter writer = new StringWriter(); final Result result = new StreamResult(writer); final DOMSource source = new DOMSource(doc); transformer.transform(source, result); return writer.getBuffer().toString().getBytes(); }
From source file:Main.java
public static byte[] documentToByteArray(Document data, Integer indent) throws TransformerException { ByteArrayOutputStream result = new ByteArrayOutputStream(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); if (indent != null) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", indent.toString()); }//w w w. j a v a 2 s.com transformer.transform(new DOMSource(data), new StreamResult(result)); return result.toByteArray(); }
From source file:Main.java
/** * Print a DOM tree to an output stream or if there is an exception while doing so, print the * stack trace.//ww w.j a v a 2 s .co m * * @param dom * @param os */ public static void printDom(Node dom, OutputStream os) { Transformer trans; PrintWriter w = new PrintWriter(os); try { TransformerFactory fact = TransformerFactory.newInstance(); trans = fact.newTransformer(); trans.transform(new DOMSource(dom), new StreamResult(new OutputStreamWriter(os))); } catch (TransformerException e) { w.println("An error ocurred while transforming the given DOM:"); e.printStackTrace(w); } }
From source file:Main.java
private static void doTransform(Source xslSource, Source xmlSource, Result xslResult) { try {/*from www . j a v a 2s.co m*/ TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(xslSource); transformer.transform(xmlSource, xslResult); } catch (TransformerFactoryConfigurationError ex) { throw new RuntimeException(ex); } catch (TransformerException ex) { throw new RuntimeException(ex); } }