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 void saveXML(Document doc, OutputStream stream) {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer;// w w w .jav a  2s. c o  m
    try {
        transformer = tFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(stream);
        transformer.transform(source, result);
        stream.flush();
        stream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void print(Document doc) {
    try {/*  w  w  w  .  j a  va 2s  . c om*/
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty("encoding", "utf-8");

        transformer.transform(new DOMSource(doc),
                new StreamResult(new OutputStreamWriter(System.out, "utf-8")));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void buildXmlFile(Document doc, String path) {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    try {//from ww  w .j  av  a2 s.  co m
        Transformer transformer = tfactory.newTransformer();
        DOMSource source = new DOMSource(doc);

        StreamResult result = new StreamResult(new File(path));
        transformer.setOutputProperty("encoding", "gb2312");
        transformer.transform(source, result);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void SaveDom(Document dom, String filepath) {
    try {//from ww  w  .j  ava 2  s.com
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        DOMSource domSource = new DOMSource(dom);

        StreamResult streamResult = new StreamResult(new FileOutputStream(filepath));
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.transform(domSource, streamResult);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String transform(Source xsl, Document doc) {
    StringWriter writer = new StringWriter();
    try {/*from  www  .  j  av  a 2s .  co m*/
        TransformerFactory.newInstance().newTransformer(xsl).transform(new DOMSource(doc),
                new StreamResult(writer));
    } catch (Exception e) {
    }
    return writer.toString();
}

From source file:Main.java

public static String transformToString(Source source) {
    try {//  www .  ja va 2s.  c om
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        StreamResult result = new StreamResult(new StringWriter());
        transformer.transform(source, result);
        return result.getWriter().toString();
    } catch (TransformerException ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Save a DOM document//from  w  w  w . j  a  va2s. co  m
 */
public static void saveDocument(Document document, OutputStream out, boolean indent) throws IOException {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        if (indent) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        }
        transformer.transform(new DOMSource(document), new StreamResult(out));
    } catch (TransformerException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:Main.java

public static void printXml(Document doc, Writer writer) throws Exception {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
}

From source file:Main.java

public static String nodeToString(Node noh) throws TransformerException {
    Transformer t = TransformerFactory.newInstance().newTransformer();
    StringWriter wr = new StringWriter();
    StreamResult result = new StreamResult(wr);

    t.transform(new DOMSource(noh), result);
    return wr.toString();
}

From source file:Main.java

public static String transText(Node doc) throws Exception {
    TransformerFactory transfactory = TransformerFactory.newInstance();
    Transformer transformer = transfactory.newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "yes");
    transformer.setOutputProperty("indent", "yes");

    Source source = new DOMSource(doc);
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    transformer.transform(source, result);
    return sw.toString();
}