importjavax.xml.transform.*;
importjavax.xml.transform.stream.*;
importjava.io.*;
publicclassxslt{
publicstaticvoidmain(Stringargs[]){
//建立transformerFactory对象
TransformerFactoryfactory=TransformerFactory.newInstance();
//以样式表
文件输出建立Transformer对象
Transformertransformer=null;
try{
transformer=factory.newTransformer(newStreamSource("cdcatalog.xsl"));
System.out.print(transformer);
}catch(TransformerConfigurationExceptione){
e.printStackTrace();//
tochangebodyofcatchstatementuseFile|Settings|FileTemplates.
}
StreamSourcexmlsource=newStreamSource("cdcatalog.xml");
//
输出streamResult创建与
输出文档html
文件的关联
StreamResultoutput=null;
ByteArrayOutputStreambaos=newByteArrayOutputStream();
output=newStreamResult(baos);
//转xml文档
try{
transformer.transform(xmlsource,output);
Stringstr=baos.toString();
System.out.println(str);
}catch(TransformerExceptione){
e.printStackTrace();//
tochangebodyofcatchstatementuseFile|Settings|FileTemplates.
}
}
}