网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  java核心代码例程之:(jaxp) sax     
  文章作者:未知  文章来源:水木森林  
  查看:125次  录入:管理员--2007-11-17  
 
import javax.xml.parsers.*;
import org.xml.sax.*;


/**
 * saxdemo uses jaxp to acquire a sax parser to parse an xml file.
 * the example xml file represents a shopping cart.
 *
 * the following jars must be in your classpath:
 * - jaxp.jar
 * - xerces.jar (for sax parser implementation)
 *
 * download jaxp (which includes these jars) here: http://java.sun.com/xml/
 * find additional xerces info here: http://xml.apache.org/
 *
 * note: unlike dom, sax parsing does not load the xml file into memory.
 * sax parsers traverse the xml file and report parse "events" to an event handler.
 **/

 public class saxdemo
  extends org.xml.sax.handlerbase
{

  /**
   * main creates and runs a saxtest instance.
   **/
  public static void main( string[] args )
  {
    saxdemo me = new saxdemo();
    me.run();
  }


  public void run()
  {
    try
    {
      saxparserfactory factory = saxparserfactory.newinstance();
      log( "saxparserfactory classname: " + factory.getclass().getname() );

      saxparser saxparser = factory.newsaxparser();
      log( "saxparser classname: " + saxparser.getclass().getname() );

      /*
      the saxparser.parse method initiates parsing of the xml file.
      the second parameter specifies which class will handle parse events.
      this class must extend org.xml.sax.handlerbase
      **/
      saxparser.parse( "cart.xml", this );
    }
    catch( exception e )
    {
      e.printstacktrace();
    }
  }

  /**
   * log simply prints the specified message to system.out
   **/
  public void log( string message )
  {
    system.out.println( message );
  }


  /**
   * saxparser calls startdocument() when it starts parsing a document
   **/
  public void startdocument ()
    throws saxexception
  {
    log( "start sax parse" );
  }

  /**
   * saxparser calls enddocument() when it finishes parsing a document
   **/
  public void enddocument ()
    throws saxexception
  {
    log( "end sax parse" );
  }

  /**
   * saxparser calls startelement() when it encounters an opening element tag (eg <item>)
   **/
  public void startelement (string name, attributelist attrs)
    throws saxexception
  {
    log( "<" + name + ">" );
  }

  /**
   * saxparser calls endelement() when it encounters a closing element tag (eg </item>)
   **/
  public void endelement (string name)
    throws saxexception
  {
    log( "</" + name + ">" );
  }

  /**
   * saxparser calls characters() when it encounters text outside of any tags
   **/
  public void characters(char[] p0, int p1, int p2)
    throws saxexception
  {
    string str = new string( p0, p1, p2 ).trim();
    if( str.length() > 0 )
      log( str );
  }


}
 
 
上一篇: java核心代码例程之:(jaxp) dom    下一篇: java核心代码例程之:(jaxp) xsl transformation
  相关文档
javadoc 利弊分析(from ibm) 11-17
java多线程程序设计入门 11-16
java workshop 11-17
hashtable简易数据库程序 11-17
ant初学入门之一:基本概念、安装与配置 11-17
j2se api读取properties文件的六种方法 01-21
java实现数据驱动的命令用户界面 11-16
介绍在java多线程编程技术中的高级应用 11-16
用db2 udb版本8开发企业java应用程序 11-17
在组件(components)之间共享ticker 11-17
成员函数的说明和使用 11-17
用swing组件实现登录对话框 11-17
体验java 1.5中面向方面(aop)编程 11-17
java标准单元测试库 junit 4 抢先看 11-17
java方法过载 11-17
jbuilder2005创建开发文档之javadoc 11-16
构建高性能j2ee应用的10个技巧 11-16
找到打包jre使jbuilder生成exe文件运行 11-17
use itext create a pdf file 11-17
java动画中消除闪烁的两个绝招! 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息