网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  java、xml与数据库编程实践 (二)     
  文章作者:未知  文章来源:水木森林  
  查看:95次  录入:管理员--2007-11-17  
 
  文件dbxmlparser.java封装了对xml文件的操作。
  
  import javax.xml.parsers.*;
  
  import org.w3c.dom.*;
  
  import org.xml.sax.*;
  
  import java.io.*;
  
  public class dbxmlparser
  
  {
  
    static string xmlfile;
  
    public dbxmlparser(string filename)
  
    {
  
      xmlfile=filename;
  
    }
  
    public static element loaddocument()
  
    {
  
      try
  
      {
  
        //工厂
  
        documentbuilderfactory dcfactory=documentbuilderfactory.newinstance();
  
        //文档构造器
  
        documentbuilder db=dcfactory.newdocumentbuilder();
  
        //构造的文档
  
        document doc=db.parse(xmlfile);
  
        //根元素
  
        element root=doc.getdocumentelement();
  
        return root;
  
        }catch( parserconfigurationexception e){
  
        system.out.println("parserconfigurationexception");
  
        e.printstacktrace();
  
      }catch(ioexception e)   {
  
        system.out.println("ioexception ");
  
        e.printstacktrace();
  
      }catch(saxexception e)   {
  
        system.out.println("saxexception ");
  
        e.printstacktrace();
  
      }catch(exception e) {
  
        e.printstacktrace();
  
      }
  
      return null;
  
    }
  
    public connpara getsource()
  
    {
  
      element root=loaddocument();
  
      if( root==null) { return null;  }
  
      nodelist nodes=root.getelementsbytagname("source");
  
      if(nodes.getlength()>0)
  
      {   
  
        node node=nodes.item(0);
  
        string connclass=getchildelementvalue(node,"class");
  
        string url=getchildelementvalue(node,"url");
  
        string username=getchildelementvalue(node,"user");
  
        string password=getchildelementvalue(node,"password");
  
        return new connpara(connclass,url,username,password);
  
      }
  
      return null;   
  
    } 
  
    public connpara getdest()
  
    {
  
      element root=loaddocument();
  
      if( root==null) return null;
  
      nodelist nodes=root.getelementsbytagname("dest");
  
      if(nodes.getlength()>0)
  
      {   
  
        node node=nodes.item(0);
  
        string connclass=getchildelementvalue(node,"class");
  
        string url=getchildelementvalue(node,"url");
  
        string username=getchildelementvalue(node,"user");
  
        string password=getchildelementvalue(node,"password");
  
        return new connpara(connclass,url,username,password);
  
      }
  
      return null;   
  
    }
  
    //得到子元素的值
  
    private string getchildelementvalue(node node,string subtagname)
  
    {
  
        string returnstring = "";
  
        if(node != null)
  
        {
  
          nodelist children = node.getchildnodes();
  
          for(int innerloop = 0; innerloop < children.getlength(); innerloop++)
  
          {
  
            node child = children.item(innerloop);
  
            if(child == null || child.getnodename() == null || !child.getnodename().equals(subtagname))
  
              continue;
  
            node grandchild = child.getfirstchild(); 
  
            if(grandchild.getnodevalue() != null)
  
              return grandchild.getnodevalue();
  
          }
  
        }
  
        return returnstring;   
  
    } 
  
  }
 
 
上一篇: java、xml与数据库编程实践 (一)    下一篇: java、xml与数据库编程实践 (三)
  相关文档
java基础:在java程序中实现回调例程 11-16
jcrontab - java定时程序进阶学习 11-17
good java style: part 1 11-17
threadlocal的设计与使用 11-17
java两则常见错误详析及解决 11-16
几个实用的servlet应用例子-入门、cookie、session及上传文件 11-17
spring framework中面向方面编程二 11-17
JAVA中用接口实现多继承和多态的方法 04-14
j2ee 和 .net之间的对比 11-17
教您如何成为 ejb 专家详解系列连载之三 11-17
业界动态 java标准迎挑战 未来由谁主宰 11-16
关于j2me midp1.0.3中的安全问题简述 11-17
java语言的socket编程(1) 11-16
制作类似于title、alt的提示效果 11-17
在实际开发中教您netbeans的使用图解 11-17
抽象类和方法 11-17
parentfolder 属性 11-16
web应用中java的速度真相探讨 11-16
一种纯java的数据库—jdatastore 11-17
ubuntu配置java+eclipse+myeclipse环境 01-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息