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

/**
 * Convert XML DOM document to a XML string representation
 *
 * @param doc//from w w w . j  ava  2  s .  com
 *            XML DOM document
 * @return XML string
 * @throws Exception
 *             in error case
 */
public static String xmlDOMDocumentToString(Document doc) throws Exception {
    if (doc == null) {
        throw new RuntimeException("No XML DOM document (null)!");
    }
    StringWriter stringWriter = new StringWriter();
    String strDoc = null;

    try {
        StreamResult streamResult = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        // transformerFactory.setAttribute("nIndent-number", new Integer(4));
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.transform(new DOMSource(doc.getDocumentElement()), streamResult);
        stringWriter.flush();
        strDoc = stringWriter.toString();
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Parsing of XML DOM document failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (stringWriter != null) {
            stringWriter.close();
            stringWriter = null;
        }
    }

    return strDoc;
}

From source file:Main.java

/**
 * Stores an XMl document into a file.//  w  w w.  ja  v a  2 s .c o  m
 *
 * @param doc xml document to be stored
 * @param location absolute path where to write down the document
 * @throws TransformerException If an unrecoverable error occurs during the
 * course of the transformation.
 */
public static void save(Document doc, String location) throws TransformerException {
    doc.normalize();
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult streamResult = new StreamResult(new File(location));
    transformer.transform(source, streamResult);
}

From source file:Main.java

public static String documentToString(Node document) {
    try {//from  w  w  w  .j  ava  2  s  .  com
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        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", "4");
        transformer.transform(new DOMSource(document), new StreamResult(sw));
        return sw.toString().replace("\r\n", "\n");
    } catch (TransformerException e) {
        throw new IllegalAccessError("Couldn't transform document to string");
    }
}

From source file:Main.java

public static String XML2String(Node node) {
    try {/*  w w w.j a v a  2 s  .  c  om*/
        StringWriter writer = new StringWriter();
        Transformer trans = TransformerFactory.newInstance().newTransformer();
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        Source source = new DOMSource(node);
        Result result = new StreamResult(writer);
        trans.transform(source, result);
        return writer.toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String XMLDocumentToString(Document _doc) {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans;//w  ww  .  j  a  v  a2 s  .  c  om
    String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    try {
        trans = transfac.newTransformer();

        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(_doc);
        trans.transform(source, result);
        xmlString += sw.toString();
    } catch (TransformerConfigurationException e) {
        System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage());
        return null;
    } catch (TransformerException e) {
        System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage());
        return null;
    }
    return xmlString;
}

From source file:Main.java

public static String getDocumentAsXml(Document doc) {
    StringWriter sw = new java.io.StringWriter();
    try {//from  www . j  a  v  a2s.co  m
        DOMSource domSource = new DOMSource(doc);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult sr = new StreamResult(sw);
        transformer.transform(domSource, sr);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return sw.toString();
}

From source file:Main.java

public static void doc2stream(Document doc, OutputStream out)
        throws TransformerConfigurationException, TransformerException {
    doc2stream(doc, new StreamResult(out));
}

From source file:Main.java

public static String getXmlPage(String xmlString, int page, String xslPath) {
    String styledResponse = "";
    StringReader rd = new StringReader(xmlString);
    StringWriter wrt = new StringWriter();
    TransformerFactory tFac = TransformerFactory.newInstance();
    try {//from w  ww.j  a v  a  2s.  c  om
        File xsl = new File(xslPath);
        Transformer transformer = tFac.newTransformer(new StreamSource(xsl));
        transformer.setParameter("Page", String.format("%s", page));
        transformer.transform(new StreamSource(rd), new StreamResult(wrt));

        styledResponse = wrt.toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return styledResponse;
}

From source file:Main.java

private static void SaveCustomerFile(Document CustomerDoc, JFrame mainFrame) {
    try {//  ww  w  .ja va 2  s  . co  m
        TransformerFactory Factory = TransformerFactory.newInstance();
        Transformer Trans = Factory.newTransformer();
        DOMSource source = new DOMSource(CustomerDoc);
        File f = new File(".");
        String FilePath = f.getAbsoluteFile().getParent() + "\\Customers.xml";
        f = new File(FilePath);
        if (f.exists()) {
            f.delete();
        }
        StreamResult Result = new StreamResult(f);
        Trans.setOutputProperty(OutputKeys.INDENT, "yes");
        Trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "7");
        Trans.transform(source, Result);
    } catch (TransformerException ex) {
        System.out.println(ex.getMessage());
        JOptionPane.showMessageDialog(mainFrame,
                "There Was an Error Saving The File. Please Restart the Application.");
        System.exit(1);
    }
}

From source file:Main.java

public static String DocToString(Document doc)
        throws TransformerFactoryConfigurationError, TransformerException {
    if (doc == null)
        return new String("");
    String xmlString = "";
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    //initialize StreamResult with File object to save to file   
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    xmlString = result.getWriter().toString();

    return xmlString;
}