Example usage for javax.xml.transform TransformerFactory newTransformer

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

Introduction

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

Prototype

public abstract Transformer newTransformer() throws TransformerConfigurationException;

Source Link

Document

Create a new Transformer that performs a copy of the Source to the Result .

Usage

From source file:Main.java

/**
 * //from  w ww .java  2s .  co m
 * @param doc
 * @return
 */
public static String document2String(Document doc) {
    try {
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        // transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void saveXML(Document paramDocument, File paramFile) throws TransformerException {
    TransformerFactory localTransformerFactory = TransformerFactory.newInstance();
    Transformer localTransformer = localTransformerFactory.newTransformer();
    localTransformer.setOutputProperty("indent", "yes");
    StreamResult localStreamResult = new StreamResult(paramFile);
    DOMSource localDOMSource = new DOMSource(paramDocument);
    localTransformer.transform(localDOMSource, localStreamResult);
}

From source file:Main.java

public static String documentToString(Document doc)
        throws TransformerConfigurationException, TransformerException, IOException {
    //return doc.getDocumentElement().toString();

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");

    DOMSource src = new DOMSource(doc);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from   ww w. ja v  a  2s . co  m*/
        StreamResult sr = new StreamResult(baos);
        transformer.transform(src, sr);

        String result = baos.toString();
        return result;
    } finally {
        baos.close();
    }
}

From source file:Main.java

/**
 * Persists the XML to the file system.//from  w  ww .j  a  v a2s . co m
 *
 * @param src the source XML.
 * @param file the file to persist to.
 * @throws TransformerException
 */
public static void persist(Source src, File file) throws TransformerException {
    final TransformerFactory tranFactory = TransformerFactory.newInstance();
    final Transformer aTransformer = tranFactory.newTransformer();
    final Result dest = new StreamResult(file);
    aTransformer.transform(src, dest);
}

From source file:Main.java

static public void outputToStream(Document document, OutputStream outputStream) throws Exception {
    // Use a Transformer for output
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    //    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(outputStream);
    transformer.transform(source, result);

}

From source file:Main.java

static public void outputToStream(Element document, OutputStream outputStream) throws Exception {
    // Use a Transformer for output
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    //     transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(outputStream);
    transformer.transform(source, result);

}

From source file:Main.java

public static void ElementToStream(Element element, OutputStream out) {
    try {//  www. j  a v  a 2s. c  o m
        DOMSource source = new DOMSource(element);
        StreamResult result = new StreamResult(out);
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        transformer.transform(source, result);
    } catch (Exception ex) {
    }
}

From source file:Main.java

/**
 * Transform xml Document to String// w w  w  .j av  a  2  s  . c om
 * @param doc Xml Document
 * @return String representation
 */
public static String getContent(Document doc) {
    try {
        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.transform(new DOMSource(doc), new StreamResult(sw));
        return sw.toString();
    } catch (Exception ex) {
        throw new RuntimeException("Error converting to String", ex);
    }
}

From source file:Main.java

public static String nodeAsString(Node node) {
    String nodeStr = "";
    TransformerFactory tff = TransformerFactory.newInstance();
    try {/*from  www.ja va  2 s . c  o m*/
        Transformer tf = tff.newTransformer();
        tf.setOutputProperty("encoding", "UTF-8");

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        tf.transform(new DOMSource(node), new StreamResult(bos));
        return bos.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return nodeStr;
}

From source file:Main.java

public static String transferXmlToString(Document doc) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {//from ww w.  jav a  2s .  c o m
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        Properties p = t.getOutputProperties();
        p.setProperty(OutputKeys.ENCODING, "UTF-8");
        t.setOutputProperties(p);
        t.transform(new DOMSource(doc), new StreamResult(bos));
    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    String xmlStr = "";
    try {
        xmlStr = bos.toString("UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return xmlStr;
}