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

public static String xmlToString(Document xml) throws TransformerConfigurationException, TransformerException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Transformer transformer = getTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.transform(new DOMSource(xml), new StreamResult(bos));
    return bos.toString();
}

From source file:Main.java

private static void write(final Node node, final Result result) {
    try {//from   ww  w  . j  a  va2  s .  c o  m
        // Prepare the DOM document for writing
        final Source source = new DOMSource(node);

        // Write the DOM document to the file
        final Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    } catch (final TransformerConfigurationException e) {
    } catch (final TransformerException e) {
    }

}

From source file:Main.java

public static String node2String(Node node) throws Exception {
    if (node == null)
        return null;
    StringWriter out = null;/*  w ww. jav  a  2s  .  c  o m*/

    try {
        Transformer transformer = transformerFactory.newTransformer();
        out = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(out));
        return out.toString();
    } catch (Exception e) {
        throw e;
    } finally {
        if (out != null)
            out.close();
    }
}

From source file:Main.java

public static void stringToResult(String source, Result result) throws IOException {

    try {//from  w ww  . j a  v  a 2s  .  c o m
        Transformer trans = transFactory.newTransformer();
        StringReader reader = new StringReader(source);
        trans.transform(new StreamSource(reader), result);
    } catch (TransformerException ex) {
        throw new IOException(ex);
    }
}

From source file:Main.java

/** Write a DOM document to an OutputStream */
public static void writeXmlDocumentToStream(Document document, OutputStream stream) throws Exception {
    Source source = new DOMSource(document);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    StreamResult result = new StreamResult(stream);
    transformer.transform(source, result);
    stream.flush();// w w w . j a  va  2 s.com
}

From source file:Main.java

public static String getAsText(Node n) {
    try {/*ww  w.j av  a2  s.  co  m*/
        Transformer t = TransformerFactory.newInstance().newTransformer();
        StringWriter out = new StringWriter();
        t.transform(new javax.xml.transform.dom.DOMSource(n), new javax.xml.transform.stream.StreamResult(out));
        return out.toString();
    } catch (Throwable t) {
        t.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static DOMResult transform(SOAPPart part) throws Exception {
    Transformer trans = TransformerFactory.newInstance().newTransformer();
    DOMResult rs = new DOMResult();
    trans.transform(part.getContent(), rs);

    return rs;//from w  w  w.ja va2s .  c  om
}

From source file:Main.java

/**
 * Write DOM document into output stream
 *
 * @param doc DOM document which will be written into OutputStream
 * @param out OutputStream into which the XML document is written
 * @param indent If true, then the XML will be indented
 *//* w ww  .jav a 2s. c o  m*/
private static void writeDocument(Document doc, java.io.OutputStream out, boolean indent) throws Exception {
    if (doc == null) {
        throw new Exception("Document is null");
    } else if (out == null) {
        throw new Exception("OutputStream is null");
    } else {
        int indentationValue = 0;
        if (indent) {
            indentationValue = INDENT_AMOUNT;
        }
        Transformer t = getXMLidentityTransformer(indentationValue);
        t.transform(new javax.xml.transform.dom.DOMSource(doc),
                new javax.xml.transform.stream.StreamResult(out));
        out.close();
    }
}

From source file:Main.java

public static synchronized void deserialize(InputStream source, Result result)
        throws TransformerConfigurationException, JAXBException, TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer;
    transformer = factory.newTransformer();
    transformer.transform(new StreamSource(source), result);
}

From source file:Main.java

/**
 * Converts an XML node list to a string
 * /*  ww  w . ja va  2  s.  c  o m*/
 * @param nodeList
 * @return a string representation of the node list
 * @throws TransformerFactoryConfigurationError
 * @throws TransformerException
 * @throws IOException
 */
public static String nodeListToString(NodeList nodeList)
        throws TransformerFactoryConfigurationError, TransformerException, IOException {
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < nodeList.getLength(); i++) {
        try (StringWriter sw = new StringWriter()) {
            Transformer serializer = TransformerFactory.newInstance().newTransformer();
            serializer.transform(new DOMSource(nodeList.item(i)), new StreamResult(sw));
            result.append(sw);
        }
    }
    return result.toString();
}