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

/**
 * Process a w3c XML document into a Java String.
 * /* w w w  .j a  va  2 s .c  o  m*/
 * @param outputDoc
 * @return
 */
public static String printToString(Document doc) {
    try {
        doc.normalizeDocument();
        DOMSource source = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(source, result);
        return writer.toString();
    } catch (Exception e) {
        // e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static void transformDocument(Document document, Writer out, File stylesheet)
        throws TransformerException {
    document.normalizeDocument();/*from www . j  a  v  a  2  s . c  om*/
    Transformer idTransform = null;
    TransformerFactory transFactory = TransformerFactory.newInstance();
    StreamSource stylesource = new StreamSource(stylesheet);
    idTransform = transFactory.newTransformer(stylesource);
    Source source = new DOMSource(document);
    Result result = new StreamResult(out);
    idTransform.transform(source, result);
}

From source file:Main.java

public static void xsl(String inFilename, String outFilename, String xslFilename) {
    try {/*from www .  jav  a 2  s  .co  m*/
        TransformerFactory factory = TransformerFactory.newInstance();
        Templates template = factory.newTemplates(new StreamSource(new FileInputStream(xslFilename)));
        Transformer xformer = template.newTransformer();
        Source source = new StreamSource(new FileInputStream(inFilename));
        Result result = new StreamResult(new FileOutputStream(outFilename));
        xformer.transform(source, result);
    } catch (FileNotFoundException e) {
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
        SourceLocator locator = e.getLocator();
        int col = locator.getColumnNumber();
        int line = locator.getLineNumber();
        String publicId = locator.getPublicId();
        String systemId = locator.getSystemId();
    }
}

From source file:Main.java

/**
 * convert an XML node to an XML statement
 * @param node        current XML node/* www  .j  av a  2  s  .c om*/
 * @return            XML string
 */
public static String nodeToString(Node node) {
    //MagicBooleans.trace("nodeToString(node: " + node + ")");
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "no");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        System.out.println("nodeToString Transformer Exception");
    }
    String result = sw.toString();
    //MagicBooleans.trace("nodeToString() returning: " + result);
    return result;
}

From source file:Main.java

public static void saveToDisk(Document doc, OutputStream out)
        throws TransformerConfigurationException, TransformerException {
    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    serializer.transform(new DOMSource(doc), new StreamResult(out));
}

From source file:Main.java

public static String getXMLString(Element elm, boolean htmlMode) throws TransformerException {
    /* TODO: Certain HTML entities, like "⇒", currently seem impossible to represent, as the
         HTML mode will output them in a named form unsupported by the Swing HTML parser
         (e.g. "⇐"). *//*from   w  w  w.j a  v  a 2  s  .c om*/
    Transformer t;
    t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.METHOD, htmlMode ? "html" : "xml");
    t.setOutputProperty(OutputKeys.INDENT, "no");
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, htmlMode ? "yes" : "no");
    StringWriter ret = new StringWriter();
    t.transform(new DOMSource(elm), new StreamResult(ret));
    return ret.toString();
}

From source file:Main.java

/**
 * Returns a string representation of {@code node}. This method should only
 * be used for debugging purposes, at it creates a new transformer factory
 * and transformer upon each call./*  ww w . j av a  2 s  . c  om*/
 * 
 * @param n
 *            the node
 * @return a string representation of {@code node}
 */
public static String toString(Node n) {
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        StringWriter sw = new StringWriter();
        t.transform(new DOMSource(n), new StreamResult(sw));
        return sw.toString();
    } catch (TransformerException e) {
        return null;
    }
}

From source file:Main.java

/**
 * method used to convert a xml document to a string
 * //  w ww  .  j a v a 2 s .c o  m
 * @param doc
 * @return
 */
public static String convertDocumentToString(Document doc) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;

    try {
        /**
         * transformation of document happens here
         */
        transformer = tf.newTransformer();
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        String output = writer.getBuffer().toString();

        /**
         * logging success
         */
        logger.info("xml conversion to string  successful Class:XMLParserUtility line# 98");

        return output;

    } catch (TransformerException e) {
        e.printStackTrace();
        logger.fatal("Could not transform document to XML", e);
    }
    return null;
}

From source file:Main.java

public static byte[] dumpToByteArray(Document document)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(bos);
    transformer.transform(new DOMSource(document), result);
    byte[] array = bos.toByteArray();

    return array;
}

From source file:Main.java

/**
 * write out an XML file//from   ww  w.  j av a2 s. c o m
 * 
 * @param doc
 * @param os
 * @throws TransformerException 
 * @throws IOException 
 */
public static void writeXML(Document doc, OutputStream os) throws TransformerException, IOException {
    // write out xml file
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

    //indent XML properly
    //formatXML(doc,doc.getDocumentElement(),"  ");

    //normalize document
    doc.getDocumentElement().normalize();

    //write XML to file
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(os);

    transformer.transform(source, result);
    os.close();
}