List of usage examples for java.io StringWriter StringWriter
public StringWriter()
From source file:Main.java
public static String elementToString(Element ele) { try {/*from w w w . jav a 2s. com*/ StringWriter sw = new StringWriter(); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(ele); StreamResult result = new StreamResult(sw); transformer.setOutputProperty("indent", "yes"); transformer.transform(source, result); return sw.toString(); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static String parseObject2XmlString(Object object) { if (object == null) { return noResult; }/*from w w w. j av a 2 s .c om*/ StringWriter sw = new StringWriter(); JAXBContext jAXBContent; Marshaller marshaller; try { jAXBContent = JAXBContext.newInstance(object.getClass()); marshaller = jAXBContent.createMarshaller(); marshaller.marshal(object, sw); return sw.toString(); } catch (Exception e) { e.printStackTrace(); return noResult; } }
From source file:Main.java
public static String streamToString(InputStream is) throws IOException { if (is != null) { Writer writer = new StringWriter(); char[] buffer = new char[1024]; try {/*from w w w . j a va2s .c o m*/ Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } finally { is.close(); } return writer.toString(); } else { return ""; } }
From source file:Main.java
public static <T> String marshal(T object) throws JAXBException { final Marshaller marshaller = JAXBContext.newInstance(object.getClass()).createMarshaller(); final StringWriter stringWriter = new StringWriter(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(object, stringWriter); return stringWriter.toString(); }
From source file:Main.java
static public String convertStreamToString(InputStream is) throws IOException { if (is != null) { Writer writer = new StringWriter(); char[] buffer = new char[1024]; try {/* www . j av a 2 s . c o m*/ Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } finally { is.close(); } return writer.toString(); } else { return ""; } }
From source file:Main.java
public static String parseObjToXmlString(Object obj) { if (obj == null) { return noResult; }//from w w w. j a v a2s . c om StringWriter sw = new StringWriter(); JAXBContext jAXBContext; Marshaller marshaller; try { jAXBContext = JAXBContext.newInstance(obj.getClass()); marshaller = jAXBContext.createMarshaller(); marshaller.marshal(obj, sw); return sw.toString(); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } return noResult; }
From source file:Main.java
public static String DocumentToString(final Document doc) { try {//from w w w.j ava 2s.c o m StringWriter writer = new StringWriter(); Result l_s = new StreamResult(writer); doc.normalize(); TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), l_s); return writer.toString(); } catch (Exception e) { System.err.println(e); return null; } }
From source file:Main.java
public static String toXml(Object object) { final StringWriter out = new StringWriter(); JAXBContext context = null;//from w w w . ja v a2 s. c om try { context = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(object, new StreamResult(out)); } catch (PropertyException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } return out.toString(); }
From source file:Main.java
public static String trans(Element ele) { StringWriter writer = new StringWriter(); try {/*w w w.java 2s . c om*/ transformer.transform(new DOMSource(ele), new StreamResult(writer)); } catch (TransformerException e) { e.printStackTrace(); } return writer.toString(); }
From source file:Main.java
public static String parseObjectToXml(Object obj) { if (obj == null) { return NO_RESULT; }/*ww w.j av a 2 s .com*/ StringWriter sw = new StringWriter(); try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.marshal(obj, sw); return sw.toString(); } catch (JAXBException e) { e.printStackTrace(); } return NO_RESULT; }