public static void transform(String data, String style, String result, String sys, String pub) { //transforms source xml file using style xsl file and saves result in result file
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
File stylesheet = new File(style);
File datafile = new File(data);
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(datafile);
TransformerFactory tFactory = TransformerFactory.newInstance();
StreamSource stylesource = new StreamSource(stylesheet); //
Transformer transformer = tFactory.newTransformer(stylesource); //ads styling information to the transformation
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //sets the encoding declared in xml-file
if (sys != null) {
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, sys); //sets the system dtd
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub); //sets the public dtd
}
DOMSource source = new DOMSource(document);
File fout = new File(result); //creates output file
StreamResult result1 = new StreamResult(new PrintWriter(fout, "UTF-8")); //ensures that file is writen in a certain encoding (should match the encoding declared earlier)
transformer.transform(source, result1);
}
... lots of catches