Вы можете сделать это с помощью JAXP API, пакет javax.xml.transform
.
Вот код, который я использую:
public static void doXsltTransform( StreamSource sourceXml, StreamSource xslt, StreamResult resultXml ) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer( xslt );
transformer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
transformer.transform( sourceXml, resultXml );
}
И вы можете вызвать это так:
InputStream inputStream = // some input stream from whereever
OutputStream out = // where you want to write the XML/HTML
StreamSource sourceXml = new StreamSource( inputStream );
StreamSource xslt = new StreamSource( inpStreamOfYourXSLT );
StreamResult resultXml = new StreamResult( out );
doXsltTransform( sourceXml, xslt, resultXml );
Это импорт:
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;