List of usage examples for java.io StringWriter StringWriter
public StringWriter()
From source file:Main.java
public static String asXml(Object object) throws IOException { try {// ww w. ja v a2 s. c o m StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(object.getClass()); Marshaller m = context.createMarshaller(); m.marshal(object, writer); return writer.toString(); } catch (Exception e) { return ""; } }
From source file:Main.java
/** * Converts an exception to a string./*from w ww . j a v a 2s . c o m*/ * @param exception * @return */ public static String getExceptionStackTraceAsString(Exception exception) { StringWriter sw = new StringWriter(); exception.printStackTrace(new PrintWriter(sw)); return sw.toString(); }
From source file:Main.java
public static String convertToXml(Object source, Class... type) { String result;//from www . j a v a 2 s. com StringWriter sw = new StringWriter(); try { JAXBContext context = JAXBContext.newInstance(type); Marshaller marshaller = context.createMarshaller(); marshaller.marshal(source, sw); result = sw.toString(); } catch (JAXBException e) { throw new RuntimeException(e); } return result; }
From source file:Main.java
public static String marshallToStringNoValidation(final Object xmlObject) throws JAXBException { StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(xmlObject.getClass()); Marshaller jaxbMarshaller = context.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(xmlObject, writer); return writer.toString(); }
From source file:Main.java
/** * get stack trace message//w w w . j av a 2 s .c om * * @param e the Exception * @return stack trace message of the Exception */ public static String getStrackTrace(Exception e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw, true)); String str = sw.toString(); return str; }
From source file:Main.java
public static String convertStreamToString(InputStream is) throws IOException { InputStreamReader r = new InputStreamReader(is); StringWriter sw = new StringWriter(); char[] buffer = new char[1024]; try {//from w w w. java 2 s. c o m for (int n; (n = r.read(buffer)) != -1;) sw.write(buffer, 0, n); } finally { try { is.close(); } catch (IOException e1) { e1.printStackTrace(); } } return sw.toString(); }
From source file:Main.java
public static String bean2XML(Object obj) throws Exception { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller m = context.createMarshaller(); StringWriter sw = new StringWriter(); m.marshal(obj, sw);//from ww w . j a v a 2s .c o m return sw.toString(); }
From source file:Main.java
private static String convertStreamToString(InputStream is) { Writer writer = new StringWriter(); char[] buffer = new char[2048]; try {// www . j av a 2 s.com Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); int len; while ((len = reader.read(buffer)) != -1) { writer.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return writer.toString(); }
From source file:Main.java
public static String toString(Object entity, boolean formatOutput) { StringWriter stringWriter = new StringWriter(); try {/*from ww w . ja v a 2 s . co m*/ Map<String, Object> properties = new HashMap<String, Object>(); properties.put(Marshaller.JAXB_FORMATTED_OUTPUT, formatOutput); JAXBContext jaxbContext = JAXBContext.newInstance(entity.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.marshal(entity, stringWriter); } catch (JAXBException e) { stringWriter.write(e.getMessage()); } return stringWriter.toString(); }
From source file:Main.java
public static String getXml(Document doc) { DOMSource doms = new DOMSource(doc); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); String xml = null;/*from w ww . ja va 2s . c o m*/ try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties properties = t.getOutputProperties(); properties.setProperty(OutputKeys.ENCODING, "GB2312"); properties.setProperty(OutputKeys.METHOD, "xml");//! properties.setProperty(OutputKeys.VERSION, "1.0"); properties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperties(properties); t.transform(doms, sr); String dtd = doc.getDoctype().getInternalSubset(); if ((null != dtd) && (dtd.length() > 0)) { dtd = "\n<!DOCTYPE Catalog [\n" + dtd + "]>\n"; } ; xml = "<?xml version=\"1.0\" encoding=\"GB2312\"?>" + dtd; xml += sw.toString(); } catch (TransformerConfigurationException tce) { //"Transformer Configuration Exception\n-----" } catch (TransformerException te) { //"Transformer Exception\n---------" } return xml; }