List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
public static void save(Document document, IFile file, IProgressMonitor monitor) throws TransformerException, CoreException, UnsupportedEncodingException { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.ENCODING, "utf-8"); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); DOMSource ds = new DOMSource(document); trans.transform(ds, sr);//from w w w .j a v a2 s. c o m String xmlString = sw.toString(); InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8")); if (file.exists()) { file.setContents(is, 0, monitor); } else { file.create(is, true, monitor); } }
From source file:Main.java
public static void writeTo(Document document, File output) { try {/*from w w w . j av a 2 s . c o m*/ TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); FileOutputStream outputstream = new FileOutputStream(output); StreamResult result = new StreamResult(outputstream); // Manually add xml declaration, to force a newline after it. String xmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; outputstream.write(xmlDeclaration.getBytes()); // Remove whitespaces outside tags. // Essential to make sure the nodes are properly indented. XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); ++i) { Node node = nodeList.item(i); node.getParentNode().removeChild(node); } // Pretty-print options. transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(source, result); outputstream.close(); } catch (TransformerException | IOException | XPathExpressionException e) { System.out.println("Failed to write document file" + output.getPath() + ": " + e.toString()); } }
From source file:Main.java
/** * Formats a xml string//ww w . ja v a2 s . c o m * @param input * @param indent * @return */ public static String prettyFormatXml(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 (Exception e) { e.printStackTrace(); } return input; }
From source file:Main.java
/** * Convert the document to an array of bytes. * * @param doc The XML document./*from ww w .j av a 2s .c om*/ * @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 { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); DOMSource source = new DOMSource(doc); transformer.transform(source, result); return writer.getBuffer().toString().getBytes(); }
From source file:Main.java
public static void transform(Source source, Result res) throws Exception { TransformerFactory.newInstance().newTransformer().transform(source, res); }
From source file:Main.java
public static void save(String paramString, Document paramDocument) throws Exception { DOMSource localDOMSource = new DOMSource(paramDocument); File localFile1 = new File(paramString); File localFile2 = localFile1.getParentFile(); localFile2.mkdirs();/*from w ww . ja v a 2 s . com*/ StreamResult localStreamResult = new StreamResult(localFile1); try { TransformerFactory localTransformerFactory = TransformerFactory.newInstance(); Transformer localTransformer = localTransformerFactory.newTransformer(); Properties localProperties = localTransformer.getOutputProperties(); localProperties.setProperty("encoding", "UTF-8"); localProperties.setProperty("indent", "yes"); localTransformer.setOutputProperties(localProperties); localTransformer.transform(localDOMSource, localStreamResult); } catch (TransformerConfigurationException localTransformerConfigurationException) { localTransformerConfigurationException.printStackTrace(); } catch (TransformerException localTransformerException) { localTransformerException.printStackTrace(); } }
From source file:Main.java
public static String formatDOM(Node node) throws TransformerException { StringWriter writer = new StringWriter(); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer;//from www .ja v a2 s. c o m transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // encoding doesn't matter since we stick with strings transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.toString(); }
From source file:Main.java
/** * Writes an XML document to an output stream. * * @param document//from ww w . jav a2s. co m * @param outputStream * @throws TransformerException if the output failed */ public static void writeDocument(Document document, OutputStream outputStream, String encoding) throws TransformerException { Source source = new DOMSource(document); Result result = new StreamResult(outputStream); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty("indent", "yes"); // make the output easier to read, see Transformer.getOutputProperties transformer.setOutputProperty("encoding", encoding); transformer.transform(source, result); }
From source file:Main.java
/** * Serialized a node to a string. XML declarations will be omitted. * /*from ww w. ja v a2 s .co m*/ * @param node The node to be serialized. * * @return The serialized node. * * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static String nodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty("omit-xml-declaration", "yes"); StringWriter sw = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static void saveXMLDocument(Document document, File file) { FileOutputStream output = null; try {/*from w w w. ja va2s. c o m*/ output = new FileOutputStream(file); Source source = new DOMSource(document); Result result = new StreamResult(output); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(source, result); } catch (Exception ex) { try { output.close(); } catch (IOException io) { } } finally { try { output.close(); } catch (IOException io) { } } }