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

/**
 * marshal document object into outputStream.
 * /*w  ww  . j  ava 2  s  .com*/
 * @param sourceDocument
 * @param targetOutputStream
 * @throws ParserConfigurationException
 * @throws TransformerConfigurationException
 * @throws TransformerException
 */
public static void marshal(Document sourceDocument, OutputStream targetOutputStream)
        throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    DOMSource source = new DOMSource(sourceDocument);
    StreamResult result = new StreamResult(targetOutputStream);
    transformer.transform(source, result);
}

From source file:edu.unc.lib.dl.util.SOAPUtil.java

private static void print(SOAPMessage msg, StreamResult result) {
    try {/*from   w  w  w  .ja  va  2s.c  om*/
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        Source sourceContent = msg.getSOAPPart().getContent();
        //StreamResult result = new StreamResult(out);
        transformer.transform(sourceContent, result);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    } catch (SOAPException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static InputStream createInputStream(Element element) throws IOException {
    byte[] ret = null;
    try {//from w w  w. j a v a  2  s .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.INDENT, "yes");
        transformer.transform(source, result);
        ret = writer.toString().getBytes("UTF-8");
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return new ByteArrayInputStream(ret);
}

From source file:Main.java

/** Write a DOM to a file. */
public static void writeXML(Document document, File file) throws IOException {
    // save the DOM to file
    StreamResult result = new StreamResult(new BufferedOutputStream(new FileOutputStream(file)));
    TransformerFactory transFactory = TransformerFactory.newInstance();
    try {/*w  w  w.j a  v a  2  s  .c  om*/
        Transformer transformer = transFactory.newTransformer();
        transformer.setOutputProperty("indent", "yes");
        transformer.transform(new DOMSource(document), result);
    } catch (TransformerConfigurationException ex) {
        throw new IOException(ex.getMessage());
    } catch (TransformerException ex) {
        throw new IOException(ex.getMessage());
    }
    result.getOutputStream().close();
}

From source file:Main.java

/**
 * Converts a node to a string.//from  ww w .  ja  v a2  s  .  c  o  m
 * @param node The node to convert.
 * @return A string representation of the node.
 */
public static String getStringFromNode(Node node) {
    DOMSource domSource = new DOMSource(node);
    ByteArrayOutputStream baos = null;
    try {
        baos = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(baos);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(domSource, result);
        baos.flush();
        return new String(baos.toByteArray());
    } catch (Exception e) {
        throw new RuntimeException("Failed to stream node to string", e);
    } finally {
        try {
            baos.close();
        } catch (Exception e) {
        }
    }

}

From source file:Main.java

public static String nodeToString(final Node node) {
    final TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer;//  w  ww  .j  av  a 2 s  . co m
    try {
        transformer = transFactory.newTransformer();
        final StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(node), new StreamResult(buffer));
        buffer.append("\n");
        return buffer.toString();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void writeContentIntoXMLFile(Document doc, File file) {
    try {//w w  w.ja va 2 s .  c o m
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(file.getAbsolutePath());
        transformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException("Error occured while writing content into XML file");
    }

}

From source file:Main.java

public static String XMLtoString(Document doc) throws Exception {
    try {/*from  ww  w. jav a  2 s  .  com*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = tf.newTransformer();
        // below code to remove XML declaration
        // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (TransformerException te) {
        throw new Exception(te.getMessageAndLocation());
    }
}

From source file:Main.java

public static String convertToString(Node node) {
    boolean withXMLDeclaration = true;
    String result;//from  w  w  w . j  a  v a  2  s . c  o m
    if (withXMLDeclaration) {
        Document document = node.getOwnerDocument();
        DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
        LSSerializer serializer = domImplLS.createLSSerializer();
        result = serializer.writeToString(node);
    } else {
        try {
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transformer = transFactory.newTransformer();
            StringWriter buffer = new StringWriter();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.transform(new DOMSource(node), new StreamResult(buffer));
            result = buffer.toString();
        } catch (TransformerConfigurationException e) {
            result = "";
        } catch (TransformerException e) {
            result = "";
        }
    }
    return result;
}

From source file:Main.java

/**
 * write this as an XML document in a stream
 * @param out the stream//from   ww  w .  j  av  a 2 s  . co  m
 * @param omitdecl should we omit the declaration
 */
static public void print(Node node, Writer out, boolean omitdecl) {
    try {
        if (node == null)
            return;

        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty("omit-xml-declaration", (omitdecl ? "yes" : "no"));
        DOMSource source = new DOMSource(node);
        StreamResult result = new StreamResult(out);
        transformer.transform(source, result);
    } catch (Exception e) {
        throw new RuntimeException("Cannot print Node", e);
    }

}