Example usage for javax.xml.transform.stream StreamResult StreamResult

List of usage examples for javax.xml.transform.stream StreamResult StreamResult

Introduction

In this page you can find the example usage for javax.xml.transform.stream StreamResult StreamResult.

Prototype

public StreamResult(File f) 

Source Link

Document

Construct a StreamResult from a File.

Usage

From source file:Main.java

/**
 * Serialize an XML document into a String (for debug purpose only!). the
 * returned String could contains an error message instead if a problem as
 * occurred during the serialization. This method is intended for debug /
 * trace purpose because you really don't need to serialize XML at all in
 * your code unless your planing to output it to a file. In this case, use
 * {@link XmlHelper#writeXmlToFile(Document, File)} instead.
 *//*  ww w .  j  a v  a  2 s  .  c o m*/
public static String dumpXml(Document document) {
    TransformerFactory factory = TransformerFactory.newInstance();
    try {
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (TransformerException e) {
        return "XML dump failed: " + e.getMessage();
    }
}

From source file:Main.java

/**
 * src:http://www.journaldev.com/1237/java-convert-string-to-xml-document-and-xml-document-to-string
 * @param doc//from  w ww.j a v a 2 s .  c o  m
 * @return
 */
public static String convertDocumentToString(Document doc) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;
    try {
        transformer = tf.newTransformer();
        // below code to remove XML declaration
        // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        return output;
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static String toHTML(String xml) throws TransformerException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(baos);
    xmlToHTMLTransformer.transform(new StreamSource(new ByteArrayInputStream(xml.getBytes())), result);
    return baos.toString();
}

From source file:Main.java

/**
 * Converts a document object to an xml string
 * @param document the document to convert
 * @param documentTransformer the DOM document transformer
 * @return the xml string// w w w .  j  a v  a 2s .co m
 */
public static String documentToString(Document document, Transformer documentTransformer)
        throws TransformerException {
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(document);
    documentTransformer.transform(source, result);
    return sw.toString();
}

From source file:Main.java

public static String parse(Document document) {
    try {//w w w .  ja  va2 s  .  co  m
        TransformerFactory dbf = TransformerFactory.newInstance();
        Transformer t = dbf.newTransformer();
        t.setOutputProperty("encoding", "utf-8");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        t.transform(new DOMSource(document), new StreamResult(bos));
        return bos.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String GetStringFromDoc(Document d) {
    /*/*from   ww w  .  ja  va2 s  .c  om*/
     DOMImplementationLS domImplementation = (DOMImplementationLS) d.getImplementation();
     LSSerializer lsSerializer = domImplementation.createLSSerializer();
            
     return lsSerializer.writeToString(d);
     */
    StringWriter output = new StringWriter();

    try {
        //Transformer transformer = TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl",null).newTransformer();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(d.getDocumentElement()), new StreamResult(output));
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return output.toString();
}

From source file:Main.java

public static void save(Document doc, String fileName) {
    save(doc, new StreamResult(fileName));
}

From source file:Main.java

public static void print(Node node, OutputStream out)
        throws UnsupportedEncodingException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

    transformer.transform(new DOMSource(node), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}

From source file:Main.java

/**
 * write out an XML file/*from  w ww. j a v a2s. c  o  m*/
 * 
 * @param doc
 * @param os
 * @throws TransformerException 
 * @throws IOException 
 */
public static void writeXML(Document doc, OutputStream os) throws TransformerException, IOException {
    // write out xml file
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

    //indent XML properly
    //formatXML(doc,doc.getDocumentElement(),"  ");

    //normalize document
    doc.getDocumentElement().normalize();

    //write XML to file
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(os);

    transformer.transform(source, result);
    os.close();
}

From source file:Main.java

public static String getStringFromDocument(Document doc) throws Exception {
    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();
}