List of usage examples for javax.xml.transform Transformer transform
public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException;
Transform the XML Source
to a Result
.
From source file:net.sf.jabref.exporter.OpenOfficeDocumentCreator.java
private static void exportOpenOfficeCalcXML(File tmpFile, BibDatabase database, List<BibEntry> entries) { OOCalcDatabase od = new OOCalcDatabase(database, entries); try (Writer ps = new OutputStreamWriter(new FileOutputStream(tmpFile), StandardCharsets.UTF_8)) { DOMSource source = new DOMSource(od.getDOMrepresentation()); StreamResult result = new StreamResult(ps); Transformer trans = TransformerFactory.newInstance().newTransformer(); trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.transform(source, result); } catch (Exception e) { throw new Error(e); }// www .ja v a 2s .c o m }
From source file:Main.java
/** * * @param source/*from w ww . ja v a 2 s .co m*/ * @param xsltSource * @return * @throws TransformerConfigurationException * @throws JAXBException * @throws TransformerException */ public static synchronized String getElementValue(InputStream source, InputStream xsltSource) throws TransformerConfigurationException, JAXBException, TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer; transformer = factory.newTransformer(new StreamSource(xsltSource)); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.transform(new StreamSource(source), result); return sw.toString(); }
From source file:Main.java
public static boolean ExportXML(Document xmlOutput, String filename) { try {/* www .j av a2 s. co m*/ if (xmlOutput != null) { Source source = new DOMSource(xmlOutput); FileOutputStream file = new FileOutputStream(filename); StreamResult res = new StreamResult(file); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, "ISO8859-1"); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.transform(source, res); file.close(); return true; } return false; } catch (FileNotFoundException e) { return false; } catch (TransformerConfigurationException e) { return false; } catch (TransformerException e) { return false; } catch (IOException e) { return false; } }
From source file:Main.java
public static String toString(Element e) { try {//from w w w . j a v a 2 s . co m TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer xform = tfactory.newTransformer(); Source src = new DOMSource(e); java.io.StringWriter writer = new StringWriter(); Result result = new javax.xml.transform.stream.StreamResult(writer); xform.transform(src, result); return writer.toString(); } catch (Exception ex) { return "Unable to convert to string: " + ex.toString(); } }
From source file:Main.java
/** * Save document to a file/* www .j av a2 s. c o m*/ * * @param document : Document to be saved * @param fileName : Represent file name to save * @throws Exception */ public static void saveDocumentTo(Document document, String fileName) throws Exception { File encryptionFile = new File(fileName); try (FileOutputStream fOutStream = new FileOutputStream(encryptionFile)) { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(fOutStream); transformer.transform(source, result); } }
From source file:Main.java
/** * Save an XML document into a file/* w ww.j a v a 2s . 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 void createXML222(String xmlFile, String xpath, String element, String data) { try {/* w w w . j av a 2 s. c om*/ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); Element rootElement = document.createElement(xpath); document.appendChild(rootElement); for (int i = 1; i <= 1; i++) { Element em = document.createElement(element); em.appendChild(document.createTextNode(data)); rootElement.appendChild(em); } 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
public static String XML2String(Node node) { try {// w w w. ja va 2 s .com StringWriter writer = new StringWriter(); Transformer trans = TransformerFactory.newInstance().newTransformer(); trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); Source source = new DOMSource(node); Result result = new StreamResult(writer); trans.transform(source, result); return writer.toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String prettyPrintXML(String source) { try {/*w ww . j a v a 2s .co m*/ ByteArrayOutputStream baos = new ByteArrayOutputStream(); TransformerFactory tff = TransformerFactory.newInstance(); Transformer tf; tf = tff.newTransformer(); tf.setOutputProperty(OutputKeys.INDENT, "yes"); tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StreamResult result = new StreamResult(baos); tf.transform(new StreamSource(new StringReader(source)), result); return new String(baos.toByteArray()); } catch (Exception e) { return source; } }
From source file:com.net2plan.utils.HTMLUtils.java
/** * Converts an XML file to a formatted HTML output via an XSLT definition. * //from w w w. j a va2 s . co m * @param xml String containing an XML file * @param xsl URL containing an XSLT definition * @return Formatted HTML output */ public static String getHTMLFromXML(String xml, URL xsl) { try { Source xmlDoc = new StreamSource(new StringReader(xml)); Source xslDoc = new StreamSource(xsl.openStream()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(xslDoc); transformer.transform(xmlDoc, new StreamResult(baos)); String html = baos.toString(StandardCharsets.UTF_8.name()); html = prepareImagePath(html, xsl); return html; } catch (IOException | TransformerFactoryConfigurationError | TransformerException e) { throw new RuntimeException(e); } }