Java XML Transform transform(String xmlInURI, String xslInURI)

Here you can find the source of transform(String xmlInURI, String xslInURI)

Description

transform

License

Apache License

Declaration

public static String transform(String xmlInURI, String xslInURI) throws Exception 

Method Source Code


//package com.java2s;
/* //from w  w w  . j av  a 2 s  . c  o m
 *
 * Copyright  2012, 2013  Freescale Semiconductor
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
*/

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class Main {
    public static String transform(String xmlInURI, String xslInURI) throws Exception {
        String retValue = null;
        try {
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Templates translet = tFactory.newTemplates(new StreamSource(xslInURI));
            Transformer transformer = translet.newTransformer();
            DocumentBuilderFactory docFact = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuild = docFact.newDocumentBuilder();
            Document doc = docBuild.parse(xmlInURI);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Result result = new StreamResult(bos);
            transformer.transform(new DOMSource(doc), result);
            retValue = bos.toString("UTF-8");
        } catch (TransformerConfigurationException tce) {
            throw new TransformerConfigurationException(tce);
        } catch (ParserConfigurationException pce) {
            throw new ParserConfigurationException();
        } catch (IOException ioe) {
            throw new IOException(ioe);
        } catch (TransformerException te) {
            throw new TransformerException(te);
        } catch (SAXException se) {
            throw new SAXException(se);
        }
        return retValue;
    }
}

Related

  1. transform(Source source, Templates cachedXSLT)
  2. transform(Source xmlSource, Result outputTarget)
  3. transform(Source xmlSource, Templates template, Result result, Map parameters)
  4. transform(Source xsl, Source xml, Result result)
  5. transform(String xmlInputFile, String xsltInputFile, OutputStream outputStream)
  6. transform(String xmlsource, File xsltfile)
  7. transform(String xslSource, Node original)
  8. transform(Transformer transformer, Node element)
  9. transformar(String xmlOrigen, String xslOrigen)