Example usage for java.io StringWriter StringWriter

List of usage examples for java.io StringWriter StringWriter

Introduction

In this page you can find the example usage for java.io StringWriter StringWriter.

Prototype

public StringWriter() 

Source Link

Document

Create a new string writer using the default initial string-buffer size.

Usage

From source file:Main.java

public static String NodeToStr(Node node) throws Exception, TransformerFactoryConfigurationError {
    StringWriter sw = new StringWriter();
    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    serializer.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

public static String transformString(String xmlString, String xsltFilePath) throws Exception {
    StreamResult result = new StreamResult(new StringWriter());
    StreamSource s = new StreamSource(new File(xsltFilePath));
    StreamSource xml = new StreamSource(new StringReader(xmlString));

    Transformer transformer = TransformerFactory.newInstance().newTransformer(s);
    transformer.transform(xml, result);//from w ww .  j  av  a2  s .c  o  m

    String response = result.getWriter().toString();

    return response;
}

From source file:Main.java

static String getXMLString(Document xmlDoc) throws Exception {
    StringWriter writer = null;/*from w w  w  . j a  v  a  2  s .  co m*/
    DOMSource source = new DOMSource(xmlDoc.getDocumentElement());
    writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.transform(source, result);
    StringBuffer strBuf = writer.getBuffer();
    return strBuf.toString();
}

From source file:Main.java

public static String toString(Node doc) throws Exception {
    DOMSource domSource = new DOMSource(doc);
    StreamResult result = new StreamResult(new StringWriter());
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    //serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"users.dtd");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.transform(domSource, result);
    return result.getWriter().toString();
}

From source file:Main.java

public static String getDefaultCurrencies(InputStream is) {

    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    try {//from  w  w  w  . ja v a2 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);
        }
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, "loadCurrencies " + "Encoding error");
    } catch (IOException e) {
        Log.e(TAG, "loadCurrencies " + "IOException");
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            Log.e(TAG, "loadCurrencies " + "IOException - close");
        }
    }
    return writer.toString();
}

From source file:Main.java

public static String Doc2String(Document paramDocument) {
    try {//from ww  w. j  ava 2s  . c o m
        DOMSource localDOMSource = new DOMSource(paramDocument);
        StringWriter localStringWriter = new StringWriter();
        StreamResult localStreamResult = new StreamResult(localStringWriter);
        TransformerFactory localTransformerFactory = TransformerFactory.newInstance();
        Transformer localTransformer = localTransformerFactory.newTransformer();
        localTransformer.transform(localDOMSource, localStreamResult);
        return localStringWriter.toString();
    } catch (Exception localException) {
        localException.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static <T> String toString(T xml) {
    JAXBContext jc;//from  w  w  w .j  a v a 2s .  co m
    try {
        jc = JAXBContext.newInstance(xml.getClass());
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        StringWriter writer = new StringWriter();
        m.marshal(xml, writer);
        return writer.toString();
    } catch (JAXBException e) {
        return "";
    }
}

From source file:Main.java

public static void print(Node dom) {
    try {//from ww w.  jav a 2 s  .co  m
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer t = factory.newTransformer();
        StringWriter sw = new StringWriter();
        t.transform(new DOMSource(dom), new StreamResult(sw));
        System.out.println(sw.toString());
    } catch (Exception ex) {
        System.out.println("Could not print XML document");
    }
}

From source file:Main.java

public static String documentToXmlString(Document document) {
    DOMSource domSource = new DOMSource(document);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;//  w ww  .  j a v  a2  s. c o  m
    try {
        transformer = tf.newTransformer();
        transformer.transform(domSource, result);
    } catch (TransformerException e) {
        throw new RuntimeException(e.getMessage());
    }
    return writer.toString();
}

From source file:Main.java

public static String docToString(Document doc) {
    try {/*from  w w w.  j a v a2 s .co  m*/
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (TransformerException ex) {
        throw new RuntimeException(ex);
    }
}