网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  java核心代码例程之:(jaxp) dom     
  文章作者:未知  文章来源:水木森林  
  查看:131次  录入:管理员--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
  相关文档
探索 corba 技术的应用领域 11-16
java中使用final修饰符需要注意的地方 11-17
早期起源 11-16
aop benchmark 11-17
人物专访: 畅销作家harold《 java i/o 》 11-17
如何使用runtime.addshutdownhook 11-17
jxta platform java参考实现源代码分析(1) 11-17
开源项目关于web app的log4j应用 11-17
使用mock对象进行单元测试 11-17
jndi查找示例 11-17
设计模式之interpreter(解释器) 11-17
enterprise javabeans导论2 11-17
实现方案的隐藏 11-17
netbeans库管理器,添加jar包、src和javadoc文档 11-17
weblogic server实现eos负载均衡 11-17
java对象持久化技术之hibernate入门之一 (1) 11-17
struts框架技术在j2ee中的研究和应用 11-16
java高级编程之displaytag学习摘要 11-17
为什么用 “==” 与 “equal”得到的输出不同 11-17
使用java语言来更新xml文档的常用方法 11-16
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息