Example usage for javax.xml.transform Transformer transform

List of usage examples for javax.xml.transform Transformer transform

Introduction

In this page you can find the example usage for javax.xml.transform Transformer transform.

Prototype

public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException;

Source Link

Document

Transform the XML Source to a Result.

Usage

From source file:Main.java

/**
 * Converts an XML document to a formatted XML string.
 * /*from ww  w . j a  va 2  s  .  co  m*/
 * @param doc The document to format.
 * @return Formatted XML document.
 */
public static String toString(Document doc) {
    if (doc == null) {
        return "";
    }

    try {
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (Exception e) {
        return e.toString();
    }
}

From source file:Main.java

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

    // Prepare the output file
    File file = new File(filename);
    Result result = new StreamResult(file);

    // 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);

    return file;//w w  w. jav  a  2  s  . c  o  m
}

From source file:Main.java

/**
 * To output a DOM as a stream./*from w  ww .j  a v  a 2 s  . c o  m*/
 */
public static InputStream documentToPrettyInputStream(Node document) throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    Source xmlSource = new DOMSource(document);
    Result outputTarget = new StreamResult(outputStream);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.transform(xmlSource, outputTarget);

    InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
    return is;
}

From source file:Main.java

/**
 * marshal document object into outputStream.
 * //from w w  w. ja va  2 s .c o m
 * @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:tud.time4maps.request.CapabilitiesRequest.java

/**
 * This method is a help method to print xml documents on console.
 * /*www . j  a v a2s.c om*/
 * @param doc - the document, that should be printed on console
 */
public static void printXML(Document doc) {
    try {
        DOMSource source = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer;

        transformer = factory.newTransformer();
        transformer.transform(source, result);
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Convert an <code>org.w3c.dom.Node</code> to a <code>String</code> without including any XML header information.
 * /*from ww w  . ja  v  a 2  s . c  o m*/
 * @param node the <code>org.w3c.dom.Node</code> to convert to a String.
 * @return a <code>String</code> representing the the <code>org.w3c.dom.Node</code>
 * @throws TransformerException if there was a problem with the conversion
 */
public static String nodeToString(Node node) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    StringWriter stringWriter = new StringWriter();
    transformer.transform(new DOMSource(node), new StreamResult(stringWriter));

    return stringWriter.toString();
}

From source file:Main.java

public static String toString(Element element) {
    try {// w w w  .j  a  v  a  2s. c om
        Transformer transformer = TransformerFactory.newInstance().newTransformer();

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

        StringWriter buffer = new StringWriter();
        transformer.transform(new DOMSource(element), new StreamResult(buffer));

        return buffer.toString();

    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

private static boolean writeTo(Document doc, String fileName) throws Exception {
    boolean isOver = false;
    DOMSource doms = new DOMSource(doc);
    File f = new File(fileName);
    StreamResult sr = new StreamResult(f);
    try {// w ww.j a va  2  s .com
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        Properties properties = t.getOutputProperties();
        properties.setProperty(OutputKeys.ENCODING, "UTF-8");
        t.setOutputProperties(properties);
        t.transform(doms, sr);
        isOver = true;
    } catch (TransformerConfigurationException tce) {
        tce.printStackTrace();
    } catch (TransformerException te) {
        te.printStackTrace();
    }
    return isOver;
}

From source file:Main.java

/**
 * Transform a DOM Node to String./* ww w. j  a  v a2 s . c o m*/
 * @param node The Node to be transformed.
 * @return a String representation.
 * @throws ParserConfigurationException Parser confiuration exception
 * @throws TransformerException Transformer exception
 */
public static String toString(final Node node) throws ParserConfigurationException, TransformerException {
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    StringWriter writer = new StringWriter();
    DOMSource source = new DOMSource(node);
    StreamResult result = new StreamResult(writer);
    transformer.transform(source, result);
    return writer.toString();
}

From source file:Main.java

public static void writeContentIntoXMLFile(Document doc, File file) {
    try {/*ww  w . java 2  s. co 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");
    }

}