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 String Doc2String(Document paramDocument) {
    try {/*from  w w w .j a  v  a2 s.co  m*/
        DOMSource localDOMSource = new DOMSource(paramDocument);
        StringWriter localStringWriter = new StringWriter();
        StreamResult localStreamResult = new StreamResult(localStringWriter);
        TransformerFactory localTransformerFactory = TransformerFactory.newInstance();
        Transformer localTransformer = localTransformerFactory.newTransformer();
        localTransformer.transform(localDOMSource, localStreamResult);
        return localStringWriter.toString();
    } catch (Exception localException) {
        localException.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String printXMLNode(Node node) {
    try {/*from   w w  w .jav  a  2s.c o m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", 2);
        Transformer transformer = tf.newTransformer();
        String omitDeclaration = node instanceof Document
                || node == node.getOwnerDocument().getDocumentElement() ? "no" : "yes";
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        output = removeEmptyLines(output); // .replaceAll("\n|\r", "");
        return output;
    } catch (TransformerException te) {
        throw new RuntimeException(te);
    }
}

From source file:Main.java

public static String xml2Str(Document document) {

    try {// ww  w  .  ja  va2  s.  c  om
        DOMSource source = new DOMSource(document);
        StringWriter writer = new StringWriter();
        Result result = new StreamResult(writer);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.transform(source, result);
        return (writer.getBuffer().toString());

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

    return "";
}

From source file:Main.java

public static StringBuffer transfer(Element element, StreamSource source) {
    try {//from   w w  w  .  j ava 2s  .co  m
        StringWriter sw = new StringWriter();
        Transformer trans = null;
        if (source != null)
            trans = TransformerFactory.newInstance().newTransformer(source);
        else
            trans = TransformerFactory.newInstance().newTransformer();
        trans.transform(new DOMSource(element), new StreamResult(sw));
        sw.close();
        return sw.getBuffer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

static public void toHtml(Document xmldoc, OutputStream os) throws TransformerException {
    StreamResult out = new StreamResult(os);
    DOMSource domSource = new DOMSource(xmldoc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "html");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(domSource, out);
}

From source file:Main.java

private static void writeTreeToXMLFile() {

    try {/*from   w  ww  . j  a  va  2  s.c  o  m*/
        Source source = new DOMSource(dom);

        File file = new File("resources/Styles.xml");
        Result result = new StreamResult(file);

        Transformer xformer = TransformerFactory.newInstance().newTransformer();

        xformer.transform(source, result);
        System.out.println("End of write method");

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

}

From source file:Main.java

/**
 * output result to object//ww  w . ja  v  a 2 s . co  m
 * @param ds input DOM Source
 * @param sr output stream result
 */
public static void OutputXmlStream(DOMSource ds, StreamResult sr) {
    Transformer t;
    try {
        t = TransformerFactory.newInstance().newTransformer();
        t.transform(ds, sr);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String prettyFormat(String input, int indent, boolean isOmitXmlDeclaration) {
    try {/*w  w w .  j  a  v  a  2 s .c o m*/
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        if (isOmitXmlDeclaration) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e); // simple exception handling, please review it
    }
}

From source file:Main.java

public static String transferXmlToString(Document doc) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {//from www .  ja  v a2  s  .  co 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;
}

From source file:Main.java

/**
 * a debug method/*from w w w. j ava 2s . co  m*/
 *
 * @param node            A node to be dumped to a string
 * @param omitDeclaration A boolean whether to omit the XML declaration
 * @return A string representation of the node.
 * @throws Exception If anything goes wrong. Error handling omitted.
 */
public static String dumpNode(Node node, boolean omitDeclaration) throws Exception {
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    if (omitDeclaration) {
        xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }
    StringWriter sw = new StringWriter();
    Result result = new StreamResult(sw);
    Source source = new DOMSource(node);
    xformer.transform(source, result);
    return sw.toString();
}