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

/**
 * method used to convert a xml document to a string
 * /*  w  w w  .  ja v a  2s.c  o  m*/
 * @param doc
 * @return
 */
public static String convertDocumentToString(Document doc) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;

    try {
        /**
         * transformation of document happens here
         */
        transformer = tf.newTransformer();
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        String output = writer.getBuffer().toString();

        /**
         * logging success
         */
        logger.info("xml conversion to string  successful Class:XMLParserUtility line# 98");

        return output;

    } catch (TransformerException e) {
        e.printStackTrace();
        logger.fatal("Could not transform document to XML", e);
    }
    return null;
}

From source file:Main.java

public static String transform(String xmlString, String stylesheetPathname) throws TransformerException {
    try {/*from  w  w w  . j  a  v  a 2 s  .com*/
        TransformerFactory factory = TransformerFactory.newInstance();
        Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile());
        Transformer transformer = factory.newTransformer(stylesheetSource);
        Source inputSource = new StreamSource(new StringReader(xmlString));
        Writer outputWriter = new StringWriter();
        Result outputResult = new StreamResult(outputWriter);
        transformer.transform(inputSource, outputResult);
        return outputWriter.toString();
    } catch (TransformerConfigurationException tce) {
        throw new TransformerException(tce.getMessageAndLocation());
    } catch (TransformerException te) {
        throw new TransformerException(te.getMessageAndLocation());
    }
}

From source file:Main.java

public static String toString(Document doc, String encoding, boolean indent)
        throws TransformerFactoryConfigurationError, TransformerException {
    Source source = new DOMSource(doc);
    StringWriter sw = new StringWriter();
    Result result = new StreamResult(sw);

    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.ENCODING, encoding);

    if (indent) {
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    }/*  w  ww . jav  a  2  s .co m*/

    xformer.transform(source, result);

    return sw.getBuffer().toString();
}

From source file:Main.java

protected static Result internalTransformWithParams(Reader doc, Templates templates, Result r, boolean trace,
        String[] params) {//from www.  j a v  a2 s  .c  o  m
    StringWriter sw = new StringWriter();

    try {
        Transformer transformer = templates.newTransformer();
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length; i++)
                transformer.setParameter("param_" + i, params[i]);
        }

        transformer.transform(new StreamSource(doc), r);

        sw.close();
        return r;
    } catch (Throwable th) {
        th.printStackTrace();
        return r;
    }

}

From source file:Main.java

public static String getDomDocumentAsXml(org.w3c.dom.Document domDocument) {
    try {//  w w w .  jav  a 2 s  . c o m
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult xmlOutput = new StreamResult(new StringWriter());
        transformer.transform(new DOMSource(domDocument), xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String prettyPrintXML(String xml) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StreamResult result = new StreamResult(new StringWriter());
    StreamSource source = new StreamSource(new StringReader(xml));
    transformer.transform(source, result);
    return result.getWriter().toString();
}

From source file:Main.java

/**
 * Transforms an xml-file (xmlFilename) using an xsl-file (xslFilename) and
 * writes the output into file (outputFilename).
 * //  w  ww.ja v  a 2s.  c  o  m
 * @param xmlFile
 *            File: the xml-source-file to be transformed
 * @param xslFile
 *            File: the xsl-file with the transformation rules
 * @param outputFilename
 *            String: the name of the file the result will be written to
 */
public static void applyXSL(File xmlFile, File xslFile, String outputFilename) {
    try {
        // DocumentBuilderFactory docBFactory = DocumentBuilderFactory
        // .newInstance();
        // DocumentBuilder docBuilder = docBFactory.newDocumentBuilder();
        StreamSource xslStream = new StreamSource(xslFile);
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer(xslStream);
        StreamSource xmlStream = new StreamSource(xmlFile);
        StreamResult result = new StreamResult(new BufferedWriter(new FileWriter(outputFilename)));
        transformer.transform(xmlStream, result);
        result.getWriter().close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:Main.java

public static boolean creteEntity(Object entity, String fileName) {
    boolean flag = false;
    try {//  w  w  w . ja  v  a2 s  .c  o m
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(fileName);

        Class clazz = entity.getClass();
        Field[] fields = clazz.getDeclaredFields();
        Node EntityElement = document.getElementsByTagName(clazz.getSimpleName() + "s").item(0);

        Element newEntity = document.createElement(clazz.getSimpleName());
        EntityElement.appendChild(newEntity);

        for (Field field : fields) {
            field.setAccessible(true);
            Element element = document.createElement(field.getName());
            element.appendChild(document.createTextNode(field.get(entity).toString()));
            newEntity.appendChild(element);
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(document);
        StreamResult streamResult = new StreamResult(new File(fileName));
        transformer.transform(domSource, streamResult);
        flag = true;
    } catch (ParserConfigurationException pce) {
        System.out.println(pce.getLocalizedMessage());
        pce.printStackTrace();
    } catch (TransformerException te) {
        System.out.println(te.getLocalizedMessage());
        te.printStackTrace();
    } catch (IOException ioe) {
        System.out.println(ioe.getLocalizedMessage());
        ioe.printStackTrace();
    } catch (SAXException sae) {
        System.out.println(sae.getLocalizedMessage());
        sae.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return flag;
}

From source file:jlib.xml.XMLUtil.java

public static Document LoadXML(Source file) {
    try {/*from   w  w  w  .  j  a  v  a 2  s .c o m*/
        DocumentBuilderFactory dbf = DocumentBuilderFactoryImpl.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();
        Result res = new DOMResult(doc);
        TransformerFactory tr = TransformerFactory.newInstance();
        Transformer xformer = tr.newTransformer();
        xformer.transform(file, res);

        return doc;
    } catch (Exception e) {
        String csError = e.toString();
        Log.logImportant(csError);
        Log.logImportant("ERROR while loading XML " + file.toString());
    }
    return null;
}

From source file:Main.java

public static String transform(String xmlString, String stylesheetPathname) throws Exception {
    try {//  w ww. j av  a  2  s . c om
        TransformerFactory factory = TransformerFactory.newInstance();
        Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile());
        Transformer transformer = factory.newTransformer(stylesheetSource);
        Source inputSource = new StreamSource(new StringReader(xmlString));
        Writer outputWriter = new StringWriter();
        Result outputResult = new StreamResult(outputWriter);
        transformer.transform(inputSource, outputResult);
        return outputWriter.toString();
    } catch (TransformerConfigurationException tce) {
        throw new Exception(tce.getMessageAndLocation());
    } catch (TransformerException te) {
        throw new Exception(te.getMessageAndLocation());
    }
}