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 void serialize(Document doc, OutputStream out, Transformer transformer)
        throws TransformerException {
    Source source = new DOMSource(doc);
    Result result = new StreamResult(out);

    transformer.transform(source, result);
}

From source file:Main.java

public static void writeDocument(Document document, File file) throws TransformerException {
    TransformerFactory transformFactory = TransformerFactory.newInstance();
    Transformer idTransform = transformFactory.newTransformer();
    Source input = new DOMSource(document);
    Result output = new StreamResult(file);
    idTransform.transform(input, output);
}

From source file:Main.java

/**
 * /*w  ww .j  a v  a2 s . c o m*/
 * comment : 
 * @param doc
 * @param filename
 * @author Huynh Minh Duc
 */
public static void writeXmlFile(Document doc, String filename) {
    try {
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // 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.INDENT, "yes");
        xformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
    }
}

From source file:com.pentaho.repository.importexport.PDIImportUtil.java

public static String asXml(Document document) {
    try {//from ww w.  java  2  s.c  o m
        Source source = new DOMSource(document.getParentNode());
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        return null;
    } catch (TransformerException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String newStringFromDocument(Document doc)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
    return output;
}

From source file:Main.java

public static String getXmlPage(String xmlString, int page, String xslPath) {
    String styledResponse = "";
    StringReader rd = new StringReader(xmlString);
    StringWriter wrt = new StringWriter();
    TransformerFactory tFac = TransformerFactory.newInstance();
    try {//from w w w .j av a 2 s .  c om
        File xsl = new File(xslPath);
        Transformer transformer = tFac.newTransformer(new StreamSource(xsl));
        transformer.setParameter("Page", String.format("%s", page));
        transformer.transform(new StreamSource(rd), new StreamResult(wrt));

        styledResponse = wrt.toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return styledResponse;
}

From source file:Main.java

public static String XMLToString(Document doc) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    return writer.toString();
}

From source file:de.tudarmstadt.ukp.shibhttpclient.Utils.java

public static String xmlToString(XMLObject aObject) throws IOException {
    Document doc;/*ww w. ja v a 2  s .com*/
    try {
        doc = Configuration.getMarshallerFactory().getMarshaller(aObject).marshall(aObject).getOwnerDocument();
    } catch (MarshallingException e) {
        throw new IOException(e);
    }

    try {
        Source source = new DOMSource(doc);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (TransformerException e) {
        throw new IOException(e);
    }
}

From source file:Main.java

/**
 * Creates a string representation of a {@link Node} instance. This method
 * does not introduce any character to the string representation of the
 * {@link Node} (eg. \n or \r characters)
 *
 * @param node A {@link Node} instance/*from   w w w .ja  va 2s . c om*/
 * @return A string representation of the node instance
 * @throws RequestSecurityTokenException
 */
public static String xmlToString(Node node) throws Exception {
    try {
        Source source = new DOMSource(node);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();

    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        throw new Exception("Cannot build a string representation of the assertion.");
    } catch (TransformerException e) {
        e.printStackTrace();
        throw new Exception("Cannot build a string representation of the assertion.");
    }
}

From source file:Main.java

private static void transform(Document doc, StreamResult result) throws TransformerException {
    DOMSource source = new DOMSource(doc);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
    transformer.transform(source, result);
}