package book.xml;
import java.io.File;
import java.util.Properties;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class JAXPTransform {
public static String xml_xslt_html(String xmlFileName,String xslFileName,String htmlFileName)throws Exception{
TransformerFactory tFactory=TransformerFactory.newInstance();
StreamSource source=new StreamSource(new File(xslFileName));
Transformer tx=tFactory.newTransformer(source);
Properties properties=tx.getOutputProperties();
properties.setProperty(OutputKeys.ENCODING,"GB2312");
properties.setProperty(OutputKeys.METHOD,"html");
tx.setOutputProperties(properties);
StreamSource xmlSource=new StreamSource(new File(xmlFileName));
File targetFile=new File(htmlFileName);
StreamResult result=new StreamResult(targetFile);
tx.transform(xmlSource,result);
return targetFile.getAbsolutePath();
}
public static void main(String[] args) throws Exception {
String xmlFileName="student.xml";
String xslFileName="student.xsl";
String targetFileName="student.html";
JAXPTransform.xml_xslt_html(xmlFileName, xslFileName, targetFileName);
}
}