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 writeXmlFile(Document file, String fileName) throws TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    DOMSource source = new DOMSource(file);
    StreamResult result = new StreamResult(new File(fileName));
    transformer.transform(source, result);
}

From source file:ac.uk.diamond.sample.HttpClientTest.Utils.java

static String xmlToString(XMLObject aObject) throws IOException {
    Document doc;//from w  w w.j a 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

public static String toXML(Document dom, Properties outputProperties) {
    try {//from  w  ww .  j av a 2s.com
        DOMSource source = new DOMSource(dom);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        Transformer tx = TransformerFactory.newInstance().newTransformer();
        if (outputProperties != null) {
            tx.setOutputProperties(outputProperties);
        }
        tx.transform(source, result);
        String s = writer.toString();
        writer.close();
        return s;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String writeXmlToStream(Document doc, Result streamResult) {
    String result = "";
    try {//from  www .j a  v a 2s .c om
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // Write the DOM document to the file
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer xformer = tFactory.newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        xformer.transform(source, streamResult);
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
    }
    return result;
}

From source file:Main.java

public static void writeXMLDocToFile(Document outputDoc, String outFileName) {
    try {//  w w  w  .  jav a 2 s . c om
        //FileWriter writer = new FileWriter( outFileName );
        final String encoding = "UTF-8";
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outFileName), encoding);
        // Use a Transformer for output
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();

        DOMSource source = new DOMSource(outputDoc);
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Could not create output XML file: " + outFileName);
    } catch (TransformerConfigurationException tce) {
        // Error generated by the parser
        System.out.println("* Transformer Factory error");
        System.out.println("  " + tce.getMessage());

        // Use the contained exception, if any
        Throwable x = tce;
        if (tce.getException() != null)
            x = tce.getException();
        x.printStackTrace();
    } catch (TransformerException te) {
        // Error generated by the parser
        System.out.println("* Transformation error");
        System.out.println("  " + te.getMessage());

        // Use the contained exception, if any
        Throwable x = te;
        if (te.getException() != null)
            x = te.getException();
        x.printStackTrace();
    }
}

From source file:Main.java

public static void xsl(String inFilename, String outFilename, String xslFilename) {
    try {/*  w ww  .  j  a  v  a2 s . c  o m*/
        TransformerFactory factory = TransformerFactory.newInstance();
        Templates template = factory.newTemplates(new StreamSource(new FileInputStream(xslFilename)));
        Transformer xformer = template.newTransformer();
        Source source = new StreamSource(new FileInputStream(inFilename));
        Result result = new StreamResult(new FileOutputStream(outFilename));
        xformer.transform(source, result);
    } catch (FileNotFoundException e) {
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
        SourceLocator locator = e.getLocator();
        int col = locator.getColumnNumber();
        int line = locator.getLineNumber();
        String publicId = locator.getPublicId();
        String systemId = locator.getSystemId();
    }
}

From source file:Main.java

/**
 * Get standard xml string of node//from  www . j  a v  a  2 s. co m
 * 
 * @param node
 * @return
 */
public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return sw.toString();
}

From source file:Main.java

private static void formatXML(Source xmlSource, Source xslSource, Writer output) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    Templates t = factory.newTemplates(xslSource);
    Transformer transformer = t.newTransformer();
    StreamResult result = new StreamResult(output);
    transformer.transform(xmlSource, result);
}

From source file:Main.java

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

    // Prepare the output file
    Result result = new StreamResult(file.toURI().getPath());

    // 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;/*from  w w  w.j a  v a 2  s . com*/
}

From source file:Main.java

/**
 * To output a DOM as a stream.//from w ww.  j  a v a 2s. c  o  m
 */
public static InputStream documentToPrettyInputStream(Document 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;
}