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

/**
 * domdemo uses jaxp to acquire a documentbuilder to build a dom document from 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 and dom object implementations)
 *
 * download jaxp (which includes these jars) here: http://java.sun.com/xml/
 * find additional xerces info here: http://xml.apache.org/
 *
 **/

public class domdemo
{

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

      documentbuilder builder = factory.newdocumentbuilder();
      system.out.println( "documentbuilder classname: " + builder.getclass().getname() );

      //parse the xml file and create the document
      document document = builder.parse( "cart.xml" );

      /*
      at this point, all data in the xml file has been parsed and loaded into memory
      in the form of a dom document object. the document is a tree of node objects.
      this printnode() method simply recurses through a node tree and displays info
      about each node.**/
      printnode( document, "" );

    }
    catch( exception e )
    {
      e.printstacktrace();
    }
  }



  /**
   * printnode is a recursive method that prints info about each node
   * in a node tree to system.out. call it with the root node of your node tree and
   * an initial indent of ""
   **/

  public static void printnode( node node, string indent )
  {
    string text = null;

    if( node.getnodetype() == node.text_node )
      text = node.getnodevalue().trim();
    else
      text = node.getnodename();

    if( text.length() > 0 )
      system.out.println( indent + getnodetypename( node ) + ": " + text );

    nodelist childnodes = node.getchildnodes();
    for( int i = 0; i < childnodes.getlength(); i++ )
      printnode( childnodes.item( i ), indent + "  " );

  }

  /**
   * getnodetypename returns a string containing the type-name of the specified node.
   **/
  public static string getnodetypename( node node )
  {
    switch( node.getnodetype() )
    {
      case node.text_node:
        return "text";
      case node.element_node:
        return "element";
      case node.attribute_node:
        return "attribute";
      case node.entity_node:
        return "entity";
      case node.document_node:
        return "document";
      case node.cdata_section_node:
        return "cdata_section";
      case node.comment_node:
        return "comment";
      case node.notation_node:
        return "notation";
      case node.processing_instruction_node:
        return "processing instruction";
      case node.document_fragment_node:
        return "document fragment";
      case node.entity_reference_node:
        return "entity reference";
    }

    return "unknown node type";
  }

}
 
 
上一篇: java核心代码例程之:(jaxp) cart.xml    下一篇: java核心代码例程之:(jaxp) sax
  相关文档
新手入门:jsp基本语法与简单表单处理 11-16
新手入门:java数据库基本操作指南 11-17
jboss与sun的java标准之争日趋严重 11-17
javascript实例教程(20) ole automation(5) 11-16
blind和shutter的比较 11-17
在j2me游戏使用“精灵”-sprite(图) 11-17
jdk5.0新特性generic types (泛型) 11-17
java中浅复制与深复制 11-17
java编程语言中启动器如何查找类的说明 04-07
jbuilder9里web服务介绍 11-17
利弊分析——浅析开源的现实作用 11-17
rmi规范--第四章 11-17
jsp动态网站环境搭建应用中详细步骤介绍 (1) 06-27
分享搞定的clob字段存取的代码 11-17
zlib压缩算法在java与delphi间交互实现 11-16
eclipse开发工具使用指南 11-16
java 类型提升的约定 11-17
java类基础 11-17
j2ee开发n层应用 11-17
一步一步学习midp2。0游戏编程(三) 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
厦门(总部):13616026886 福州:0591-87655121
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息