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

public static String createString(Element element) {
    try {/*from  w  w  w.j a  va  2s.  c  o  m*/
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        DOMSource source = new DOMSource(element);
        Writer writer = new StringWriter();
        Result result = new StreamResult(writer);
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source, result);
        return writer.toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String formatDOM(Node node) throws TransformerException {
    StringWriter writer = new StringWriter();
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;// w  w  w .ja  v a2  s  .co m
    transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    // encoding doesn't matter since we stick with strings
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    transformer.transform(new DOMSource(node), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static void printDocument(final Document doc) {
    try {/*w  w  w . j ava  2s.co m*/
        final TransformerFactory tf = TransformerFactory.newInstance();
        Transformer 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.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(doc),
                new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));
    } catch (final TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (final UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (final TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String documentToString(Document document) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {//from w  ww  .java  2  s.c  o m
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    try {
        transformer.transform(new DOMSource(document), new StreamResult(writer));
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
    return output;
}

From source file:org.springsource.ide.eclipse.commons.content.core.util.ContentUtil.java

public static Transformer createTransformer() throws CoreException {
    TransformerFactory factory = TransformerFactory.newInstance();
    try {// www.ja  v a2  s  .c o  m
        return factory.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new CoreException(
                new Status(Status.ERROR, ContentPlugin.PLUGIN_ID, "Could not create transformer", e));
    }

}

From source file:Main.java

public static String toStringFromDoc(Element elem) {
    String result = null;//  www .ja  v  a2 s .  c  om

    StringWriter strWtr = new StringWriter();
    StreamResult strResult = new StreamResult(strWtr);
    TransformerFactory tfac = TransformerFactory.newInstance();
    try {
        javax.xml.transform.Transformer t = tfac.newTransformer();
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html,
        // text
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        t.transform(new DOMSource(elem), strResult);
    } catch (Exception e) {
        System.err.println("XML.toString(Document): " + e);
    }
    result = strResult.getWriter().toString();
    try {
        strWtr.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

/**
 * Print a Node tree recursively./*w w  w .  j a  v  a 2  s .com*/
 * 
 * @param node
 *            A DOM tree Node
 * @return An xml String representation of the DOM tree.
 */
public static String print(Node node) {
    if (node == null) {
        return null;
    }

    try {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty("omit-xml-declaration", "yes");
        DOMSource source = new DOMSource(node);
        ByteArrayOutputStream os = new ByteArrayOutputStream(2000);
        StreamResult result = new StreamResult(os);
        transformer.transform(source, result);
        return os.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Format the provided XML input./*  w w w.  j  av  a  2 s.  c o m*/
 * 
 * @param input
 *            XML input to format.
 * @param indent
 *            Indentation to use on formatted XML.
 * @return Formatted XML.
 * @throws TransformerException
 *             if some problem occur while processing the xml
 * @see #prettyFormat(String)
 */
public static String prettyFormat(String input, Integer indent) throws TransformerException {
    if (input != null) {
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
                String.valueOf(indent == null ? 2 : indent));
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    }
    return input;
}

From source file:Main.java

public static void printNode(OutputStream out, Node node, boolean prettyPrint, boolean includeXmlDeclaration) {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer serializer;//from www. java2  s  . com
    try {
        serializer = tfactory.newTransformer();
        if (prettyPrint) {
            //Setup indenting to "pretty print"
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        }
        if (!includeXmlDeclaration) {
            serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }

        DOMSource xmlSource = new DOMSource(node);
        StreamResult outputTarget = new StreamResult(out);
        serializer.transform(xmlSource, outputTarget);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String formatXMLStr(String xml) {
    String output = null;//w ww . j a v a 2  s .co  m
    try {
        Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new ByteArrayInputStream(xml.getBytes())).getDocumentElement();

        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(writer));

        // output = writer.getBuffer().toString().replaceAll("\n|\r", "");
        output = writer.getBuffer().toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return output;
}