Vb教程   Vb.net教程   Vfp教程   C/C++教程   Vc/Vc++教程   Delphi教程   Java教程   Powerbuilder
  杀毒频道 | 短信频道 | 网络电视 | 论文中心 | 教你学上网 | 教你学软件 | 网页特效 | 电脑基础 | 论坛  
  计算机等级 | 程序员考试 | 英语四六级 | 职称英语 | 司法考试 | 报关员考试 | 公务员考试 | 翻译员考试 | 注册会计师  
  Html教程 | Css教程 | Xml教程 | Asp教程 | Asp.net | Php教程 | Jsp教程 | Linux教程 | QQ技巧  
Photoshop Illustrator ImageReady Maya教程 3D Max教程 Lightscape Coredraw教程 Authorware Autocad教程 Freehand教程
Access教程 Mysql教程 Sql server Oracle教程 Word教程 Excel教程 Powerpoint Frontpage Asp.net源码 Php源代码
Flash教程 Fireworks Dreamweaver C#教程 outlook教程 系统安装 vbscript教程 Javascript Jsp源代码 Asp源代码
您的位置:首页 >> Java教程 >> 正文

Java、XML与数据库编程实践(二)

文章来源:csdn 作者:sirix

文件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;      

    }  
}

[返回]

     

首页 | 设为首页 | 加入收藏 | 关于本站 | 友情链接 | 版权声明

     
 
Copyright© www.bianceng.cn Powered by 编程入门网 All Rights Reserved
吉ICP备06005558号