服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java核心代码例程之:(jaxp) xsl transformation


import javax.xml.transform.*;
import javax.xml.transform.stream.*;

/**
 * transformdemo uses jaxp to acquire an xml transformer. it uses the transformer
 * to transform an xml shopping cart into an html view of the shopping cart.
 * the transformer uses transform instructions in an xslt (.xsl) file.
 *
 * the following jars must be in your classpath:
 * - jaxp.jar
 * - xerces.jar (for sax parser and dom object implementations)
 * - xalan.jar (for xslt implementation)
 *
 * download jaxp (which includes these jars) here: http://java.sun.com/xml/
 * find additional xerces and xalan info here: http://xml.apache.org/
 *
 * note: xslt authoring/programming is beyond the scope of this tutorial.
 * you"ll find good xsl info here: http://www.w3.org/style/xsl/#learning
 **/

public class transformdemo
{

  public static void main( string[] args  )
  {
    try
    {
      transformerfactory factory = transformerfactory.newinstance();
      system.out.println( "transformerfactory classname: " + factory.getclass().getname() );

      transformer transformer = factory.newtransformer( new streamsource( "cart.xsl" ) );
      system.out.println( "transformer classname: " + transformer.getclass().getname() );

      //this single line applies the xsl file to transform the xml into html.
      transformer.transform( new streamsource( "cart.xml" ), new streamresult( system.out ) );
    }
    catch( exception e )
    {
      e.printstacktrace();
    }

 }

}

扫描关注微信公众号