List of usage examples for java.io StringWriter StringWriter
public StringWriter()
From source file:Main.java
public static <T extends Object> String marshalAsString(Class clz, T marshalObj) throws JAXBException { JAXBContext context = JAXBContext.newInstance(clz); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); StringWriter writer = new StringWriter(); marshaller.marshal(marshalObj, new BufferedWriter(writer)); return writer.toString(); }
From source file:Main.java
private static byte[] serializeXML(Transformer transformer, Source input) throws UnsupportedEncodingException, TransformerException { StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); transformer.transform(input, result); String streamString = stringWriter.getBuffer().toString(); byte[] byteArray = streamString.getBytes("UTF-8"); return byteArray; }
From source file:Main.java
public static String serializeXML(Node e) throws Exception { StringWriter result = new StringWriter(); serializeXML(e, result);/*from w w w .j ava 2s. c o m*/ return result.toString(); }
From source file:Main.java
private static String xmlToString(Node node) { try {//from w w w . ja va 2 s . c o m 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 String nodeToString(final Node node, final boolean omitXMLDecl) { final StringWriter writer = new StringWriter(); final Transformer transformer; try {//from w w w . j a va 2 s . co m transformer = TransformerFactory.newInstance().newTransformer(); } catch (final TransformerException e) { throw new AssertionError("No errors expected when creating an identity transformer", e); } if (omitXMLDecl) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } else { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); } try { transformer.transform(new DOMSource(node), new StreamResult(writer)); } catch (final TransformerException e) { throw new AssertionError("No errors expected during identity transformation", e); } return writer.toString(); }
From source file:Main.java
public static void save(Document document, IFile file, IProgressMonitor monitor) throws TransformerException, CoreException, UnsupportedEncodingException { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.ENCODING, "utf-8"); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); DOMSource ds = new DOMSource(document); trans.transform(ds, sr);//from w w w . j a v a2s. c om String xmlString = sw.toString(); InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8")); if (file.exists()) { file.setContents(is, 0, monitor); } else { file.create(is, true, monitor); } }
From source file:com.autentia.tnt.util.BeanUtils.java
/** * Devuelve la pila de una excepcion//from w ww . j ava 2s . c o m * @param ex la excepcion * @return la pila de la excepcion */ public static String getErrorDesc(Throwable ex) { StringWriter sw = new StringWriter(); ex.printStackTrace(new PrintWriter(sw)); //NOSONAR return sw.toString(); }
From source file:Main.java
public static String marshal(JAXBContext context, Object object, Map<String, Object> properties) throws JAXBException { StringWriter writer = new StringWriter(); marshal(context, object, writer, properties); return writer.toString(); }
From source file:Main.java
static public String getPrettyPrint(Document doc) { try {//from www .j a v a2 s.c o m 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)); return writer.getBuffer().toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** Write a DOM document to a string */ public static String xmlDocumentToString(Document document) { try {/*from w ww .j av a 2 s .c o m*/ Source source = new DOMSource(document); 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; }