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

/** 
 * Returns the XML string representation of a Node.
 * @param node Node to convert into XML string. 
 * @param omitXmlDeclaration <tt>true</tt> to remove the XML declaration from the string. 
 * @return The XML string representation of the input node. 
 * @throws TransformerConfigurationException 
 * @throws TransformerException /*from ww  w .j a v a  2 s  .  c  o  m*/
 */
public static String getXMLString(Node node, boolean omitXmlDeclaration)
        throws TransformerConfigurationException, TransformerException {
    StringWriter writer = new StringWriter();
    DOMSource domSource = new DOMSource(node);
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
    serializer.transform(domSource, result);
    return (writer.toString());
}

From source file:Main.java

public static String getFormattedXml(String xmlString) {

    // ///////////////////////////////////////////////////////////////
    //   Declarations
    // ///////////////////////////////////////////////////////////////

    Source xmlInput = null;/*from  w w w .  j  a  va  2  s  .c  o  m*/
    StringWriter stringWriter = null;
    StreamResult xmlOutput = null;

    TransformerFactory transformerFactory = null;
    Transformer transformer = null;

    String formattedXml = null;

    // ///////////////////////////////////////////////////////////////
    //   Code
    // ///////////////////////////////////////////////////////////////

    try {

        xmlInput = new StreamSource(new StringReader(xmlString));
        stringWriter = new StringWriter();
        xmlOutput = new StreamResult(stringWriter);

        transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", 4);

        transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);

        formattedXml = xmlOutput.getWriter().toString();
    } catch (Exception e) {

        // To Do: Handle Exception..
    }

    return formattedXml;
}

From source file:Main.java

/**
 * Converts a dom to a String/*from ww w.jav  a  2 s.c  o m*/
 * 
 * @param dom
 *            dom to convert
 * @param outputProperties
 *            the properties for the String representation of the XML
 * @return the dom as a String
 */
public static String writeDomToString(Document dom, Properties outputProperties) {
    try {
        StringWriter ret = new StringWriter();
        TransformerFactory transFact = TransformerFactory.newInstance();
        //                transFact.setAttribute("indent-number", 2);
        Transformer transformer = transFact.newTransformer();
        if (outputProperties != null)
            transformer.setOutputProperties(outputProperties);
        DOMSource source = new DOMSource(dom);
        StreamResult result = new StreamResult(ret);
        transformer.transform(source, result);
        return ret.toString();
    } catch (Exception e) {
        throw new RuntimeException("Could not write dom to string!", e);
    }
}

From source file:Main.java

public static Transformer getTransformer(boolean standalone, boolean indent, int indentNumber,
        boolean omitXmlDeclaration) throws TransformerException {
    TransformerFactory f = TransformerFactory.newInstance();
    f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
    if (indent) {
        f.setAttribute("indent-number", indentNumber);
    }/*from  ww w  .j a  va2s.com*/

    Transformer t = f.newTransformer();
    if (standalone) {
        t.setOutputProperty(OutputKeys.STANDALONE, "yes");
    }
    if (indent) {
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty("{xml.apache.org/xslt}indent-amount", "" + indentNumber);
    }
    if (omitXmlDeclaration) {
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }

    return t;
}

From source file:Main.java

public static void OutputXml(File in, String saveFileInPath) throws Exception {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Source xslDoc = new StreamSource("backup.xslt");
    Source xmlDoc = new StreamSource(in.getPath());
    System.out.print(in.getName() + "/n");
    String outputFileName = in.getName().split("\\.")[0];
    System.out.println(outputFileName);
    OutputStream htmlFile;//w w  w. j  a  v a  2  s. co  m
    htmlFile = new FileOutputStream(saveFileInPath + "//" + outputFileName + ".html");

    Transformer transformer = tFactory.newTransformer(xslDoc);
    transformer.transform(xmlDoc, new StreamResult(htmlFile));
}

From source file:Main.java

public static String toXmlString(Document document) {
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    Transformer transformer;/*w w w . j  av a2  s  .  c om*/
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(document.getLastChild()), result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return sw.toString();
}

From source file:Main.java

public static File saveToFile(File file, Document document) throws TransformerException {
    // Prepare the DOM document for writing
    Source source = new DOMSource(document);

    // Prepare the output file
    Result result = new StreamResult(file.toURI().getPath());

    // Write the DOM document to the file
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.transform(source, result);//from www  . j ava 2 s.co  m

    return file;
}

From source file:Main.java

/**
 * @param doc/*ww w . j av a2  s .c  om*/
 * @return
 * @throws RuntimeException
 */
public static String prettyPrint(Document doc) throws RuntimeException {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer serializer;
    StringWriter writer = new StringWriter();
    try {
        serializer = tfactory.newTransformer();

        // Setup indenting to "pretty print"
        serializer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //$NON-NLS-1$ //$NON-NLS-2$

        serializer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String nodeToString(Node node) throws Exception {
    if (node == null) {
        return null;
    }//from  ww  w . j a va2 s  .  c  o m
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    DOMSource source = new DOMSource(node);
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    transformer.transform(source, result);
    return sw.toString();
}

From source file:Main.java

private static TransformerFactory getTransformerFactory() {
    if (transformerFactory == null) {
        transformerFactory = TransformerFactory.newInstance();
    }//from  w ww .ja  va  2  s  . co  m
    return transformerFactory;
}