List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer() throws TransformerConfigurationException;
From source file:io.leishvl.core.xml.XmlHelper.java
/** * Obtains a String representation of a XML document, omitting the XML declaration. * @param document the document to be transformed into String. * @return a String representation of the specified XML document. *//*from ww w . ja va2 s. c o m*/ public static String documentToStringOmitXmlDeclaration(final Document document) { checkNotNull(document, "Uninitialized document"); String docStr; try { final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(ENCODING, UTF_8.name()); transformer.setOutputProperty(OMIT_XML_DECLARATION, "yes"); final StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer)); docStr = writer.toString(); } catch (Exception e) { docStr = null; LOGGER.warn("Failed to convert DOM document to String", e); } return docStr; }
From source file:io.leishvl.core.xml.XmlHelper.java
public static String prettyPrint(final Document document) { checkNotNull(document, "Uninitialized document"); String docStr;/*w w w . ja va 2 s . co m*/ try { final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(ENCODING, UTF_8.name()); transformer.setOutputProperty(OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(INDENT, "yes"); final StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer)); docStr = writer.toString(); } catch (Exception e) { docStr = null; LOGGER.warn("Failed to convert DOM document to String", e); } return docStr; }
From source file:Main.java
public static void exportXmlFile(ArrayList<?> listObject, String rootElement, String interfaceName, String pathSaveFile) {/*w w w .jav a2s .com*/ try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = builderFactory.newDocumentBuilder(); // creating a new instance of a DOM to build a DOM tree. Document doc = docBuilder.newDocument(); Element root = doc.createElement(rootElement); // adding a node after the last child node of the specified node. doc.appendChild(root); Element interfaceElement = null; Element child = null; Text text; for (Object obj : listObject) { Class srcClass = obj.getClass(); Field[] field = srcClass.getFields(); interfaceElement = doc.createElement(interfaceName); for (int i = 0; i < field.length; i++) { // System.out.println(field[i].getName() + ":" + // field[i].get(obj)); child = doc.createElement(field[i].getName()); text = doc.createTextNode((field[i].get(obj)).toString()); child.appendChild(text); interfaceElement.appendChild(child); } root.appendChild(interfaceElement); } // TransformerFactory instance is used to create Transformer // objects. TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // create string from xml tree StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); transformer.transform(source, result); String xmlString = sw.toString(); File file = new File(pathSaveFile); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); bw.write(xmlString); bw.flush(); bw.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:de.tudarmstadt.ukp.shibhttpclient.Utils.java
public static String xmlToString(Element doc) { StringWriter sw = new StringWriter(); try {// w w w.j a v a2s . c o m TransformerFactory tf = TransformerFactory.newInstance(); 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(sw)); return sw.toString(); } catch (TransformerException e) { LOG.error("Unable to print message contents: ", e); return "<ERROR: " + e.getMessage() + ">"; } }
From source file:Main.java
public static String prettyFormat(String input, int indent) { try {// w w w . j a v a2 s .com 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) { throw new RuntimeException(e); // simple exception handling, please review it } }
From source file:Main.java
public static String prettyPrint(Document doc) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer;//w ww . ja v a 2 s .co m StringWriter writer = new StringWriter(); try { serializer = tfactory.newTransformer(); //Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //$NON-NLS-1$ //$NON-NLS-2$ serializer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.toString(); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:com.pentaho.repository.importexport.PDIImportUtil.java
public static String asXml(Document document) { try {//w w w. ja va 2 s.co m Source source = new DOMSource(document.getParentNode()); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); return null; } catch (TransformerException e) { e.printStackTrace(); return null; } }
From source file:Main.java
private static void saveFile(String file_path, Document doc) { try {/*from w w w .ja v a 2s. c o m*/ // write the content into xml file TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer; transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(file_path)); // Output to console for testing //StreamResult result = new StreamResult(System.out); transformer.transform(source, result); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:Main.java
/** * Print the indented document./*w ww . ja va 2 s . c o m*/ * * @param stream * @param document * * @throws UnsupportedEncodingException * @throws TransformerExceptions */ public static void printXMLFormat(Document document) throws UnsupportedEncodingException, TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 4); //$NON-NLS-1$ Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$ transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ StreamResult streamResult = new StreamResult(); //$NON-NLS-1$ DOMSource source = new DOMSource(document); transformer.transform(source, streamResult); }
From source file:Main.java
/** * Serialize XML Document to string using Transformer * //from w w w . j a v a 2 s.c o m * @param doc XML Document * @return String representation of the the Document * @throws IOException */ public static String serializeToString(Node node, String encoding) throws IOException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = null; try { transformer = tFactory.newTransformer(); } catch (TransformerConfigurationException e) { throw new IOException("Unable to serialize XML document"); } transformer.setOutputProperty(OutputKeys.INDENT, "no"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); DOMSource source = new DOMSource(node); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); try { transformer.transform(source, result); } catch (TransformerException e) { throw new IOException("Unable to serialize XML document"); } writer.flush(); return writer.toString(); }