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 getXmlPage(String xmlString, int page, String xslPath) {
    String styledResponse = "";
    StringReader rd = new StringReader(xmlString);
    StringWriter wrt = new StringWriter();
    TransformerFactory tFac = TransformerFactory.newInstance();
    try {//from w  w  w  . j  a  v a  2s  .  co m
        File xsl = new File(xslPath);
        Transformer transformer = tFac.newTransformer(new StreamSource(xsl));
        transformer.setParameter("Page", String.format("%s", page));
        transformer.transform(new StreamSource(rd), new StreamResult(wrt));

        styledResponse = wrt.toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return styledResponse;
}

From source file:Main.java

public static String convertDocumentToString(Document doc) throws IOException, TransformerException {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    return (writer.toString());
}

From source file:Main.java

public static String xmlParaString(Node doc) {
    DOMSource xmlSource = new DOMSource(doc);

    StringWriter sw = new StringWriter();
    Result result = new StreamResult(sw);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer;//from w w w .  ja v  a  2 s  . c o  m

    try {
        transformer = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    }

    try {
        transformer.transform(xmlSource, result);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    return sw.toString();
}

From source file:Main.java

public static String format(String input, int indent) {
    try {//from   www.  j ava  2 s  . co 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");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void printXml(Document xml, Writer out)
        throws TransformerException, UnsupportedEncodingException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    transformer.transform(new DOMSource(xml), new StreamResult(out));
}

From source file:Main.java

/**
 * Serialize W3C document into given output stream
 * @param doc W3C document/*from   ww  w .j  a  v a 2 s  .  c  o  m*/
 * @param out OutputStream 
 * @throws TransformerException
 */
public static void print(Document doc, OutputStream out) throws TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(out);
    transformer.transform(source, result);
}

From source file:Main.java

public static String serializeElement(Element element)
        throws TransformerFactoryConfigurationError, TransformerException {
    String serializedElement = null;
    if (element != null) {
        StringWriter output = new StringWriter();

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(element), new StreamResult(output));

        serializedElement = output.toString();
    }//  w  ww.j  av  a2  s.  c om
    return serializedElement;
}

From source file:Main.java

/**
 * Get standard xml string of node//from w  w w .j av a 2 s  .c  om
 * 
 * @param node
 * @return
 */
public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return sw.toString();
}

From source file:Main.java

/**  This method dumps out a dom document to an output stream.
 *   Header information is turned off.//from   w  ww  . j a  v  a 2s  . c  o  m
 *
 *   @param doc Dom Document
 *   @param os existing outputstream you wish to write the document to.
 */
public static void DOMtoOutputStream(Document doc, OutputStream os) {
    try {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        DOMSource src = new DOMSource(doc);
        StreamResult result = new StreamResult(os);

        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        //transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.transform(src, result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String prettyPrint(File file) {
    Transformer tf;//from  w w  w.j  a v a 2s. c o  m
    try {
        tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult stringResult = new StreamResult(new StringWriter());
        tf.transform(new DOMSource(convertFileToDom(file)), stringResult);
        return stringResult.getWriter().toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}