List of usage examples for java.io StringWriter StringWriter
public StringWriter()
From source file:Main.java
/** * Converts a {@link Node node} to an XML string * * @param node the first element// ww w .ja v a 2 s.com * @return the XML String representation of the node, never null */ public static String nodeToString(Node node) { try { StringWriter writer = new StringWriter(); createIndentingTransformer().transform(new DOMSource(node), new StreamResult(writer)); return writer.toString(); } catch (TransformerException e) { throw new IllegalStateException(e); } }
From source file:Main.java
public static void printE(String tag, Exception e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw);// w w w.ja va2s .c o m Log.e(tag, sw.toString()); }
From source file:Main.java
/** * Converts a DOM document to an xml String. * * @param doc DOM document/*from ww w . jav a 2 s .c om*/ * @return xml String. * @throws TransformerException */ public static String getStringFromDomDocument(org.w3c.dom.Document doc, org.w3c.dom.Document xslt) throws TransformerException { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; if (null != xslt) { DOMSource dxsltsource = new DOMSource(xslt); transformer = tf.newTransformer(dxsltsource); } else { transformer = tf.newTransformer(); } transformer.transform(domSource, result); return writer.toString(); }
From source file:Main.java
public static String convertXMLFileToString(String fileName) { try {/* w ww .jav a 2 s. c o m*/ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream); StringWriter stw = new StringWriter(); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.transform(new DOMSource(doc), new StreamResult(stw)); return stw.toString(); } catch (Exception e) { System.out.println("Unable to load xml file: " + e.toString()); } return null; }
From source file:Main.java
public static String constructXMLString(Document dom) throws IOException { String retXMLStr = null;/*from w ww.j a va 2s . c o m*/ TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer; try { transformer = factory.newTransformer(); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); DOMSource source = new DOMSource(dom); transformer.transform(source, result); writer.close(); retXMLStr = writer.toString(); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } /* // Make a string out of the DOM object System.out.println(dom.toString()); OutputFormat format = new OutputFormat(dom); format.setIndenting(true); ByteArrayOutputStream byteoutStream = new ByteArrayOutputStream(); XMLSerializer serializer = new XMLSerializer(byteoutStream, format); serializer.serialize(dom); String retXMLStr = new String(byteoutStream.toByteArray()); byteoutStream.close(); */ return retXMLStr; }
From source file:Main.java
public static String transText(Node doc) throws Exception { TransformerFactory transfactory = TransformerFactory.newInstance(); Transformer transformer = transfactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Source source = new DOMSource(doc); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.transform(source, result); return sw.toString(); }
From source file:Main.java
/** * Convert XML Document to a String./*from w ww . j ava 2s.co m*/ * * @param node * @return */ public static String xmlToString(Node node) { try { Source source = new DOMSource(node); 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(); } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static final String prettyPrint(final Document aNode) throws Exception { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impls = (DOMImplementationLS) registry.getDOMImplementation("LS"); // Prepare the output LSOutput domOutput = impls.createLSOutput(); domOutput.setEncoding(java.nio.charset.Charset.defaultCharset().name()); StringWriter writer = new StringWriter(); domOutput.setCharacterStream(writer); LSSerializer domWriter = impls.createLSSerializer(); DOMConfiguration domConfig = domWriter.getDomConfig(); domConfig.setParameter("format-pretty-print", true); domConfig.setParameter("element-content-whitespace", true); domWriter.setNewLine("\r\n"); domConfig.setParameter("cdata-sections", Boolean.TRUE); // And finaly, write domWriter.write(aNode, domOutput);//w ww . j a v a 2s .c o m return domOutput.getCharacterStream().toString(); }
From source file:Main.java
/** * Helper method to serialize an object to XML. Requires object to be Serializable * @param entity Object to serialize// ww w .j av a 2 s . c o m * @param <T> class that implements Serializable * @return String XML representation of object * @throws IOException if errors during serialization * @throws JAXBException if errors during serialization */ public static <T extends Serializable> String getXml(T entity) throws IOException, JAXBException { StringWriter sw = new StringWriter(); if (null != entity) { JAXBContext ctx = JAXBContext.newInstance(entity.getClass()); Marshaller m = ctx.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(entity, sw); } sw.flush(); sw.close(); return sw.toString(); }
From source file:Main.java
/** * Converts a dom document to an xml String. * // ww w . ja v a2 s.c om * @param document * @return * @see XMLSerializer */ public static String toXML(final Document document) { final XMLSerializer xmlSerializer = new XMLSerializer(); final OutputFormat outputFormat = new OutputFormat(document); outputFormat.setOmitXMLDeclaration(false); outputFormat.setEncoding("UTF-8"); outputFormat.setIndenting(true); final StringWriter stringWriter = new StringWriter(); xmlSerializer.setOutputCharStream(stringWriter); xmlSerializer.setOutputFormat(outputFormat); try { xmlSerializer.serialize(document); stringWriter.flush(); stringWriter.close(); } catch (final IOException e) { throw new RuntimeException(e); } return stringWriter.toString(); }