用TransformerFactory创建Transformer对象,用newInstance()方法得到工厂实例,再用newInstance()方法得到转换器实例。最后用transform()方法即可。
package test.chap05; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; public class TestTranformer { public static final String SOURCE_XML_FILE = "E:/XML/jar-complier/Stocks.xml"; public static final String XSL_FILE = "E:/XML/jar-complier/forth.xsl"; public static final String OUT_FILE = "E:/XML/jar-complier/Stocks-forth.out.html"; public static void main(String[] args) { TransformerFactory factory = null; Transformer transformer = null; try{ factory = TransformerFactory.newInstance(); transformer = factory.newTransformer(new StreamSource(XSL_FILE)); transformer.transform(new StreamSource(SOURCE_XML_FILE),new StreamResult(OUT_FILE)); } catch(Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); } } }
原文链接:https://www.f2er.com/xml/300406.html