Example usage for javax.xml.transform TransformerFactory newInstance

List of usage examples for javax.xml.transform TransformerFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.transform TransformerFactory newInstance.

Prototype

public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError 

Source Link

Document

Obtain a new instance of a TransformerFactory .

Usage

From source file:Main.java

public static StringBuffer transformToString(Source xmlSource, Source xslSource) {
    StringWriter writer = new StringWriter();
    Transformer transformer;//from  ww  w  .  j av  a  2s.com
    try {
        if (xslSource == null) {
            transformer = TransformerFactory.newInstance().newTransformer();
        } else {
            transformer = TransformerFactory.newInstance().newTransformer(xslSource);
        }
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(xmlSource, new StreamResult(writer));
        return writer.getBuffer();
    } catch (Exception e) {
        e.printStackTrace();
        return writer.getBuffer();
    } finally {
        try {
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static String xmlToString(Document doc) {
    try {//  ww w. jav a  2 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) {
        ex.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static void DocumentToFile(final Document doc, File file) {
    // FileWriter writer = null;
    OutputStreamWriter outputStreamWriter = null;

    try {//from w ww. j ava2  s  .  c  o  m
        // writer = new FileWriter(file);

        FileOutputStream fileOutputStream = new FileOutputStream(file);
        outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");

    } catch (Exception e) {
        System.err.println(e);

        return;
    }

    //Result l_s = new StreamResult(writer);
    Result l_s = new StreamResult(outputStreamWriter);

    doc.normalize();

    try {
        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), l_s);

        outputStreamWriter.close();
        //writer.close();
    } catch (Exception e) {
        System.err.println(e);

        return;
    }
}

From source file:Main.java

public static void saveDocument(Document document, String path)
        throws TransformerConfigurationException, TransformerFactoryConfigurationError,
        TransformerFactoryConfigurationError, TransformerException, IOException {

    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    DOMSource dom = new DOMSource(document);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", 4);

    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(dom, sr);//  www.j av  a2 s  .  c o  m

    String string = sw.toString();
    FileWriter fw = new FileWriter(new File(path));
    fw.write(string);
    fw.close();
}

From source file:Main.java

/**
 * //from w  ww  . j a v a2 s . c  o m
 * comment : 
 * @param doc
 * @param filename
 * @author Huynh Minh Duc
 */
public static void writeXmlFile(Document doc, String filename) {
    try {
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // Prepare the output file
        File file = new File(filename);
        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
    }
}

From source file:Main.java

public static String writeXmlToStream(Document doc, Result streamResult) {
    String result = "";
    try {/* ww  w  . ja v  a2s .  co  m*/
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // Write the DOM document to the file
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer xformer = tFactory.newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        xformer.transform(source, streamResult);
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
    }
    return result;
}

From source file:Main.java

/**
 * Save the XML document to a file.//from  ww w .  j  a  va  2  s  .  c o m
 * 
 * @param doc
 *            The XML document to save.
 * @param file
 *            The file to save the document to.
 * @param encoding
 *            The encoding to save the file as.
 * 
 * @throws TransformerException
 *             If there is an error while saving the XML.
 */
public static void save(Document doc, String file, String encoding) throws TransformerException {
    try {
        final Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        // initialize StreamResult with File object to save to file

        final Result result = new StreamResult(new File(file));
        final DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
    } finally {

    }
}

From source file:Main.java

static public String getPrettyPrint(Document doc) {
    try {/*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));
        return writer.getBuffer().toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

/**
 * Convert XML DOM document to a XML string representation
 *
 * @param doc/*from w  w w.ja  va  2  s.  c o m*/
 *            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

public static void toText(Node node, OutputStream out)
        throws TransformerFactoryConfigurationError, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    Source source = new DOMSource(node);
    Result output = new StreamResult(out);
    transformer.transform(source, output);
}