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 returnDocAsString(Document doc) throws TransformerException {
    StringWriter sw = new StringWriter();
    TransformerFactory tfFac = TransformerFactory.newInstance();
    // use null trandformation
    Transformer tf = tfFac.newTransformer();
    tf.transform(new DOMSource(doc), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

public static void OutputXml(File in, String saveFileInPath) throws Exception {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Source xslDoc = new StreamSource("backup.xslt");
    Source xmlDoc = new StreamSource(in.getPath());
    System.out.print(in.getName() + "/n");
    String outputFileName = in.getName().split("\\.")[0];
    System.out.println(outputFileName);
    OutputStream htmlFile;//from w  w w.  ja  va  2s . com
    htmlFile = new FileOutputStream(saveFileInPath + "//" + outputFileName + ".html");

    Transformer transformer = tFactory.newTransformer(xslDoc);
    transformer.transform(xmlDoc, new StreamResult(htmlFile));
}

From source file:Main.java

public static String encodeBase64(Element elm) throws Exception {
    ByteArrayOutputStream ostream = new ByteArrayOutputStream();
    TransformerFactory transFac = TransformerFactory.newInstance();
    Transformer trans = transFac.newTransformer();
    trans.transform(new DOMSource(elm), new StreamResult(ostream));
    return (new String(Base64.encodeBase64(ostream.toByteArray(), true)));
}

From source file:Main.java

/**
 * potentially unsafe XML transformation.
 * @param source The XML input to transform.
 * @param out The Result of transforming the <code>source</code>.
 *//*from   w  w  w  .  ja  va2  s. c om*/
private static void _transform(Source source, Result out) throws TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

    // this allows us to use UTF-8 for storing data,
    // plus it checks any well-formedness issue in the submitted data.
    Transformer t = factory.newTransformer();
    t.transform(source, out);
}

From source file:Main.java

public static String convertDocumentToString(Document doc) throws IOException, TransformerException {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    return (writer.toString());
}

From source file:Main.java

public static String toString(Document doc) throws TransformerException {
    StringWriter sw = new StringWriter();
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer transformer = tfactory.newTransformer();
    transformer.transform(new DOMSource(doc), new StreamResult(sw));

    return sw.toString();
}

From source file:Main.java

public static String toXmlString(Document doc) throws Exception {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    writer.flush();/*from   www  .j a  v  a  2 s .c  o m*/

    return writer.toString();
}

From source file:Main.java

/**
 * Saves a given XML document to the given output stream.
 */// www  .  java  2s  . com
public static void writeXML(Document document, OutputStream os) throws IOException {
    DOMSource src = new DOMSource(document);
    StreamResult res = new StreamResult(os);
    TransformerFactory tf = TransformerFactory.newInstance();
    try {
        Transformer t = tf.newTransformer();
        t.transform(src, res);
    } catch (TransformerException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:Main.java

public static StringWriter getStringWriter(Document doc) throws TransformerException {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult streamResult = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, streamResult);
    return writer;
}

From source file:JAXPTransletMultipleTransformations.java

static void doTransform(Templates translet, String xmlInURI, String htmlOutURI)
        throws TransformerException, FileNotFoundException {
    // For each transformation, instantiate a new Transformer, and perform
    // the transformation from a StreamSource to a StreamResult;
    Transformer transformer = translet.newTransformer();
    transformer.transform(new StreamSource(xmlInURI), new StreamResult(new FileOutputStream(htmlOutURI)));
}