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:Main.java
public static String getXMLAsString(Document doc) throws Exception { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); return result.getWriter().toString(); }
From source file:jlib.xml.XMLUtil.java
public static Document loadXML(ByteArrayInputStream byteArrayInputStream) { try {//from w w w .java2s.co m StreamSource streamSource = new StreamSource(byteArrayInputStream); DocumentBuilderFactory dbf = DocumentBuilderFactoryImpl.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Result res = new DOMResult(doc); TransformerFactory tr = TransformerFactory.newInstance(); Transformer xformer = tr.newTransformer(); xformer.transform(streamSource, res); return doc; } catch (Exception e) { String csError = e.toString(); Log.logImportant(csError); Log.logImportant( "ERROR while loading XML from byteArrayInputStream " + byteArrayInputStream.toString()); } return null; }
From source file:Main.java
/** * Save the given document to an output stream. Output is nicely formatted, * with child elements indented by 4 spaces. * /* w ww. j a va 2 s. c o m*/ * @param doc * Document to save * @param outputStream * OutputStream to save to * @throws TransformerException * @throws IOException */ public static void saveDocumentToFormattedStream(Document doc, OutputStream outputStream) throws TransformerException, IOException { Source source = new DOMSource(doc); Result result = new StreamResult(outputStream); Transformer transformer = createTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$ transformer.transform(source, result); }
From source file:Main.java
public static String transformXmlToString(Document importPackageDocument) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException, IOException {/*from www . j a v a 2 s. com*/ TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); StringWriter writer = new StringWriter(); javax.xml.transform.Result result = new StreamResult(writer); Source source = new DOMSource(importPackageDocument); transformer.transform(source, result); writer.close(); String xml = writer.toString(); return xml; }
From source file:PayHost.Utilities.java
/** * Make the http post response pretty with indentation * * @param input Response//from w w w.j a va 2s . c o m * @param indent Indentation level * @return String in pretty format */ public static String prettyFormat(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 (IllegalArgumentException | TransformerException e) { throw new RuntimeException(e); // simple exception handling, please review it } }
From source file:Main.java
public static String getXMLAsString(Element eElement) throws Exception { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(eElement); transformer.transform(source, result); return result.getWriter().toString(); }
From source file:net.sf.jabref.exporter.OpenDocumentSpreadsheetCreator.java
private static void exportOpenDocumentSpreadsheetXML(File tmpFile, BibDatabase database, List<BibEntry> entries) { OpenDocumentRepresentation od = new OpenDocumentRepresentation(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); }//from ww w . j a va2s . c om }
From source file:Main.java
/** * * @param source//from w ww. j av a2s .co m * @param xsltSource * @return * @throws TransformerConfigurationException * @throws JAXBException * @throws TransformerException * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static synchronized Document transform(Element source, InputStream xsltSource) throws TransformerConfigurationException, JAXBException, TransformerException, ParserConfigurationException, SAXException, IOException { Document obj = null; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); if (xsltSource != null) { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer; transformer = factory.newTransformer(new StreamSource(xsltSource)); obj = dbf.newDocumentBuilder().newDocument(); Result result = new DOMResult(obj); transformer.transform(new DOMSource(source), result); } return obj; }
From source file:Main.java
/** * Get DOM as a string/* www . j a va 2 s .c o m*/ * @param doc * @return */ public static String getStringFromDoc(org.w3c.dom.Document doc) { if (doc == null) { System.out.println("XML document is null"); } 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.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://commons.omniupdate.com/dtd/standard.dtd"); transformer.transform(domSource, result); writer.flush(); return writer.toString(); } catch (TransformerException ex) { System.out.println("Transformer Exception"); // ex.printStackTrace(); return "Error in transformation"; } }
From source file:Main.java
/** * Writes a DOM document to a file./*from w ww. j a v a 2 s . c o m*/ * * @param document * DOM document. * @param fileName * target file path. */ public static void writeDOMToFile(Document document, String fileName) { FileOutputStream fos; Source source; Result result; Transformer xformer; try { fos = new FileOutputStream(fileName); source = new DOMSource(document); result = new StreamResult(fos); xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }