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 transform(String xmlFilePath, String xsltFilePath) throws Exception {
    StreamResult result = new StreamResult(new StringWriter());
    StreamSource s = new StreamSource(new File(xsltFilePath));
    StreamSource xml = new StreamSource(new File(xmlFilePath));

    Transformer transformer = TransformerFactory.newInstance().newTransformer(s);
    transformer.transform(xml, result);

    String response = result.getWriter().toString();

    return response;
}

From source file:Main.java

public static String transform(String xml, File stylesheet) throws TransformerException {
    StringWriter writer = new StringWriter();
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));
    transformer.transform(new StreamSource(xml), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static String transform(String xml, String stylesheet) throws TransformerException {
    StringWriter writer = new StringWriter();
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));
    transformer.transform(new StreamSource(xml), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static void saveToDisk(Document doc, OutputStream out)
        throws TransformerConfigurationException, TransformerException {
    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    serializer.transform(new DOMSource(doc), new StreamResult(out));
}

From source file:Main.java

public static String Doc2String(Document paramDocument) {
    try {//ww w  .j  a  v a  2 s  . c o m
        DOMSource localDOMSource = new DOMSource(paramDocument);
        StringWriter localStringWriter = new StringWriter();
        StreamResult localStreamResult = new StreamResult(localStringWriter);
        TransformerFactory localTransformerFactory = TransformerFactory.newInstance();
        Transformer localTransformer = localTransformerFactory.newTransformer();
        localTransformer.transform(localDOMSource, localStreamResult);
        return localStringWriter.toString();
    } catch (Exception localException) {
        localException.printStackTrace();
    }
    return null;
}

From source file:Main.java

/** Write a DOM document to a string */
public static String xmlDocumentToString(Document document) {
    try {/*from w w  w.jav a2s . c o  m*/
        Source source = new DOMSource(document);
        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();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String document2XmlString(Document xmldoc) {
    try {/*from   w  w w . ja v  a  2 s.  com*/
        Source src = new DOMSource(xmldoc);
        ByteArrayOutputStream bout = new ByteArrayOutputStream();

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(); // identity transformer
        transformer.transform(src, new StreamResult(bout));
        return bout.toString("UTF-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

private static String xmlToString(Node node) {
    try {//from   w ww . ja  va  2 s .c o m
        Source source = new DOMSource(node);
        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();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String serializeElement(Element element)
        throws TransformerFactoryConfigurationError, TransformerException {
    String serializedElement = null;
    if (element != null) {
        StringWriter output = new StringWriter();

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(element), new StreamResult(output));

        serializedElement = output.toString();
    }//www  . ja v a  2s .com
    return serializedElement;
}

From source file:Main.java

public static String convertXMLFileToString(String fileName) {
    try {// w  w w  . ja  v  a 2s.com
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
        Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream);
        StringWriter stw = new StringWriter();
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.transform(new DOMSource(doc), new StreamResult(stw));
        return stw.toString();
    } catch (Exception e) {
        System.out.println("Unable to load xml file: " + e.toString());
    }
    return null;
}