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 marshalToStream(Document doc, OutputStream ostream, boolean indent) throws Exception {
    TransformerFactory transFac = TransformerFactory.newInstance();
    Transformer trans = transFac.newTransformer();
    if (indent) {
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    }//  www.  ja v a 2 s  .co  m
    trans.setOutputProperty(OutputKeys.STANDALONE, "no");

    trans.transform(new DOMSource(doc), new StreamResult(ostream));
}

From source file:Main.java

public static void document2File(Document document, String encode, File file) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource(document);
    transformer.setOutputProperty("encoding", encode);
    transformer.setOutputProperty("indent", "yes");

    transformer.transform(source, new StreamResult(file));
}

From source file:Main.java

public static void init() {
    if (isInited) {
        return;//from  w  ww . ja va2  s  .  c om
    }
    try {
        tx = TransformerFactory.newInstance().newTransformer();
        tx.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        tx.setOutputProperty(OutputKeys.INDENT, "no");
        isInited = true;
    } catch (TransformerFactoryConfigurationError | TransformerConfigurationException
            | IllegalArgumentException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String DocumentToString(final Document doc) {
    try {/*from   ww  w  . ja  va2s . c  o  m*/
        StringWriter writer = new StringWriter();
        Result l_s = new StreamResult(writer);

        doc.normalize();

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

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

        return null;
    }
}

From source file:Main.java

public static void writeXmlFile(Document doc, String fileFullPath)
        throws TransformerFactoryConfigurationError, TransformerException {
    Source source = new DOMSource(doc);
    File file = new File(fileFullPath);
    Result result = new StreamResult(file);
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(source, result);//from  w  ww . j a v a2s  .  c om
}

From source file:Main.java

public static void serialize(Document document, File targetFile) throws IOException {
    try {/*ww  w .  jav  a  2s  .  c o  m*/
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.transform(new DOMSource(document), new StreamResult(targetFile));
    } catch (TransformerException e) {
        throw new IOException(e);
    }
}

From source file:Main.java

public static String NodeToStr(Node node) throws Exception, TransformerFactoryConfigurationError {
    StringWriter sw = new StringWriter();
    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    serializer.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

public static void output(Document doc, OutputStream outputStream) throws Exception {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    tFactory.setAttribute("indent-number", 4);

    Transformer transformer = tFactory.newTransformer();
    Properties outputProperties = new Properties();
    outputProperties.put(OutputKeys.INDENT, "yes");
    outputProperties.put("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperties(outputProperties);

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new OutputStreamWriter(outputStream, "UTF-8"));
    transformer.transform(source, result);
}

From source file:Main.java

public static String prettyPrintXML(String source) {
    try {//w w w.j  a  v a 2  s  .  co  m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        TransformerFactory tff = TransformerFactory.newInstance();
        Transformer tf;
        tf = tff.newTransformer();
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StreamResult result = new StreamResult(baos);
        tf.transform(new StreamSource(new StringReader(source)), result);
        return new String(baos.toByteArray());
    } catch (Exception e) {
        return source;
    }
}

From source file:Main.java

public static String createPrintableString(Document doc) {
    String s = "";
    try {//from   w  w w . j  a  v a2 s  .  c om
        // set up a transformer
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

        // create string from xml tree
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc);
        trans.transform(source, result);

        s = sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return s;
}