List of usage examples for java.io StringWriter StringWriter
public StringWriter()
From source file:Main.java
/** * Marshals a bean to XML./* ww w.jav a 2s. co m*/ * * @param bean * @return XML {@link String} * @throws JAXBException */ public static <T> String marshal(T bean) throws JAXBException { StringWriter stw = new StringWriter(); createMarshaller(bean.getClass()).marshal(bean, stw); return stw.toString(); }
From source file:Main.java
public static String toXML(Object obj) { try {/*w w w. j a v a 2 s. c o m*/ JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); StringWriter writer = new StringWriter(); marshaller.marshal(obj, writer); return writer.toString(); } catch (JAXBException e) { e.printStackTrace(); return ""; } }
From source file:Main.java
public static String xmlToString(Document doc) { try {/*from w w w . j a v a2s .c om*/ TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString().replaceAll("\n|\r", ""); return output; } catch (TransformerException exc) { throw new RuntimeException("Unable to convert XML to String"); } }
From source file:Main.java
public static String documentToString(Document document) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = null; try {/* w w w . ja va 2 s. co m*/ transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); try { transformer.transform(new DOMSource(document), new StreamResult(writer)); } catch (TransformerException e) { throw new RuntimeException(e); } String output = writer.getBuffer().toString().replaceAll("\n|\r", ""); return output; }
From source file:Main.java
public static String transform(String xmlString, String stylesheetPathname) throws Exception { try {/*from w w w . java 2 s .c o m*/ TransformerFactory factory = TransformerFactory.newInstance(); Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile()); Transformer transformer = factory.newTransformer(stylesheetSource); Source inputSource = new StreamSource(new StringReader(xmlString)); Writer outputWriter = new StringWriter(); Result outputResult = new StreamResult(outputWriter); transformer.transform(inputSource, outputResult); return outputWriter.toString(); } catch (TransformerConfigurationException tce) { throw new Exception(tce.getMessageAndLocation()); } catch (TransformerException te) { throw new Exception(te.getMessageAndLocation()); } }
From source file:Main.java
public static void printNode(Node node) { try {/*from w w w . j av a 2 s . co m*/ // Set up the output transformer TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); // Print the DOM node StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(node); trans.transform(source, result); String xmlString = sw.toString(); System.out.println(xmlString); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static String prettyPrintXML(String xml) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); StreamSource source = new StreamSource(new StringReader(xml)); transformer.transform(source, result); return result.getWriter().toString(); }
From source file:Main.java
public static String convertToXml(Object obj, String encoding) { String result = null;/*from ww w .jav a2 s . c om*/ try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); StringWriter writer = new StringWriter(); marshaller.marshal(obj, writer); result = writer.toString(); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static String getDOMString(Document doc) { String s = null;/*from w w w . j a va2 s.c o m*/ final TransformerFactory tfactory = TransformerFactory.newInstance(); try { final Transformer xform = tfactory.newTransformer(); final Source src = new DOMSource(doc); final StringWriter writer = new StringWriter(); final Result result = new StreamResult(writer); xform.transform(src, result); s = writer.toString(); } catch (final Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; }
From source file:Main.java
/** * @param <T>/*from w ww. j a va2 s. c o m*/ * the type to serialize * @param c * the class of the type to serialize * @param o * the instance containing the data to serialize * @return a string representation of the data. * @throws Exception */ @SuppressWarnings("unchecked") public static <T> String marshal(Class<T> c, Object o) throws Exception { JAXBContext ctx = JAXBContext.newInstance(c); Marshaller marshaller = ctx.createMarshaller(); StringWriter entityXml = new StringWriter(); marshaller.marshal(o, entityXml); String entityString = entityXml.toString(); return entityString; }