Description
Transform XML according to the supplied transformer
License
Apache License
Parameter
Parameter | Description |
---|
inputFile | the XML document |
transformer | the transformer to use |
out | where to write the result |
Exception
Parameter | Description |
---|
FileNotFoundException | an exception |
Declaration
public static void transform(File inputFile, Transformer transformer, Writer out) throws FileNotFoundException
Method Source Code
//package com.java2s;
/*******************************************************************************
* Copyright (c) 2010, 2012 Institute for Dutch Lexicology
*
* 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.// www. ja v a 2 s.c om
*******************************************************************************/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class Main {
public static String transform(String input, Transformer transformer) {
StringWriter out = new StringWriter();
transform(input, transformer, out);
return out.toString();
}
public static String transform(File inputFile, Transformer transformer) throws FileNotFoundException {
StringWriter out = new StringWriter();
transform(inputFile, transformer, out);
return out.toString();
}
/**
* Transform XML according to the supplied transformer
*
* @param input
* the XML document
* @param transformer
* the transformer to use
* @param out
* where to write the result
*/
public static void transform(String input, Transformer transformer, Writer out) {
transform(new StreamSource(new StringReader(input)), transformer, out);
}
/**
* Transform XML according to the supplied transformer
*
* @param inputFile
* the XML document
* @param transformer
* the transformer to use
* @param out
* where to write the result
* @throws FileNotFoundException
*/
public static void transform(File inputFile, Transformer transformer, Writer out) throws FileNotFoundException {
transform(new StreamSource(inputFile), transformer, out);
}
/**
* Transform XML according to the supplied transformer
*
* @param inputSource
* the XML document
* @param transformer
* the transformer to use
* @param out
* where to write the result
*/
public static void transform(StreamSource inputSource, Transformer transformer, Writer out) {
try {
StreamResult result = new StreamResult(out);
transformer.transform(inputSource, result);
transformer.reset();
out.flush();
} catch (TransformerException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Related
- newTransformerHandler()
- newTransformerHandler(final SAXTransformerFactory tf)
- serializeXML(Transformer transformer, Source input)
- transform(byte[] doc, URL xsltUrl, OutputStream out)
- transform(byte[] source, InputStream xslInputStream, File output)
- transform(File xslt_source, File xml_source)
- transform(final File sourceXML, final File xslt, final File targetXML)
- transform(final Source xml, final Source xslt, final Map params, final Writer err)
- transform(InputStream styleSheet, InputStream xml, Writer out)