一、目标

用xml和Xapth做一个简单的数据管理系统,对数据的进行简单的增、删、改、查询操作。如做一个简单的班级学生信息管理系统:

 

二、环境及开发工具

环境:Java

工具:Eclipse

开发包:如图

三、原理

dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它。

XPath 是一门在 XML 文档中查找信息的语言。XPath 用于在 XML 文档中通过元素和属性进行导航。XPath 使用路径表达式在 XML 文档中选取节点。节点是通过沿着路径或者 step 来选取的。

用Xpath定位到相应节点,用dom4j进行增、删、改。

 

四,类分析,如下图:

addframe:执行添加操作时显示的界面。

allfunction:对xml操作各功能的实现。

alterframe:执行修改操作时显示的界面。

deleteframe:执行删除操作时显示的界面。

mainframe:主界面

queryframe:执行查询操作时显示的界面。

 

五、主要实现如下:

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.xpath.*;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;

/**
 * @author ncw
 * create the function
 */
public class allfunction 
{

         /*
     * 获取全部信息
     */
         public String getall(){
            String allinformation="";
            try
            {
            SAXReader saxReader = new SAXReader(); 
            //saxReader.setEncoding("UTF-8");
            Document document = saxReader.read(new java.io.File("manage.xml"));
            List academylist = document.selectNodes("/school/academy" );        
            for(int i=0;i<academylist.size();i++)
            {            
                Element academyElement = (Element)academylist.get(i);                
               allinformation=allinformation+academyElement.attribute("aid").getValue();              
               allinformation+=" 学院: "+"\n";
               Iterator classiterator = academyElement.elementIterator("class");
               while(classiterator.hasNext())
               {
                   Element classElement = (Element)classiterator.next();
                   allinformation+=classElement.attribute("cid").getValue();
                   allinformation+=" 班级: "+"\n";
                   Iterator studentiterator = classElement.elementIterator("student");
                   while(studentiterator.hasNext())
                   {
                       Element studentElement=(Element)studentiterator.next();
                       allinformation+="姓名:";
                       allinformation+=studentElement.elementText("name");                                      
                       allinformation+="      性别:";
                       allinformation+=studentElement.elementText("sex");                                           
                       allinformation+="     年龄:";
                       allinformation+=studentElement.elementText("age");                       
                       allinformation+="     成绩:";
                       allinformation+=studentElement.elementText("grade");
                       allinformation+="\n";                      
                   }      
               }    
            }            
        }catch(Exception ex)
            {
                   ex.printStackTrace();
             }               
            return allinformation;
        }
/*
* 获得一个学生的信息
*/
public String getperson(String name)
{
    String person="";
    try
    {
        SAXReader saxReader = new SAXReader(); 
        Document document = saxReader.read(new java.io.File("manage.xml"));
        List classlist = document.selectNodes("/school/academy/class" ); 
        Iterator classiterator = classlist.iterator();
        while(classiterator.hasNext())
        {
            Element classElement = (Element)classiterator.next();
               Iterator studentiterator = classElement.elementIterator("student");
               while(studentiterator.hasNext())
               {
                   Element studentElement=(Element)studentiterator.next();
                   if(studentElement.elementText("name").equals(name))
                   {
                   person+="学院:"+classElement.getParent().attributeValue("aid")+"\n";
                   person+="班级:"+classElement.attributeValue("cid")+"\n";
                   person+="姓名:"+studentElement.elementText("name");
                   person+="   性别:"+studentElement.elementText("sex");
                   person+="   年龄:"+studentElement.elementText("age");
                   person+="   成绩:"+studentElement.elementText("grade");
                  }      
               }    
          }
    }catch(Exception ex)
    {
      ex.printStackTrace();
    }      
    return person;
}
       
 /*
* 删除一个学生的信息     
*/         
public boolean delete(String name)
{
     boolean deleted=false;
     try
     {
           SAXReader saxReader = new SAXReader(); 
           Document document = saxReader.read(new java.io.File("manage.xml"));            
           List  classlist = document.selectNodes("/school/academy/class");
           Iterator classiterator = classlist.iterator();
           while(classiterator.hasNext())
           {
              Element classElement = (Element)classiterator.next();
              Iterator studentiterator = classElement.elementIterator("student");
              while(studentiterator.hasNext())
              {
                  Element studentElement=(Element)studentiterator.next();                 
                  if(studentElement.elementText("name").equals(name))
                  {
                     classElement.remove(studentElement);
                     deleted=true;
                  }
               }
           }
           
           try{
               /** 格式化输出,类型IE浏览一样 */
               OutputFormat format = OutputFormat.createPrettyPrint();
               /** 指定XML编码 */
               format.setEncoding("GBK");
              /** 将document中的内容写入文件中 */
//              XMLWriter writer = new XMLWriter(new FileWriter(new File(newfilename)),format);
               //保证编码为UTF-8,支持中文写入
              XMLWriter writer = new XMLWriter(new FileOutputStream(new File("manage.xml")),format);
              writer.write(document);
              writer.close();
              
           }catch(Exception ex){
              ex.printStackTrace();
           }
       }catch(Exception ex)
         {
           ex.printStackTrace();
         }  
           return deleted;
}



/*
* 修改一个学生的信息
*/
public boolean alter(String aid,String cid,String name,String sex,String age,String grade)
{
    boolean altered=false;    
     try{
         /*
         aid=new String(aid.getBytes("ISO8859-1"),"UTF-8");
            cid=new String(cid.getBytes("ISO8859-1"),"UTF-8");
            name=new String(name.getBytes("ISO8859-1"),"UTF-8");
            sex=new String(sex.getBytes("ISO8859-1"),"UTF-8");
            age=new String(age.getBytes("ISO8859-1"),"UTF-8");
            grade=new String(grade.getBytes("ISO8859-1"),"UTF-8");
            */
           SAXReader saxReader = new SAXReader(); 
           Document document = saxReader.read(new java.io.File("manage.xml"));            
           List academylist = document.selectNodes("/school/academy" ); 
           Iterator academyiterator = academylist.iterator();
           while(academyiterator.hasNext())
           {
               Element academyElement = (Element) academyiterator.next();
              if(academyElement.attribute("aid").getValue().equals(aid));
              Iterator classiterator = academyElement.elementIterator("class");                           
              while(classiterator.hasNext())
              {
                  Element classElement = (Element)classiterator.next();
                if(classElement.attribute("cid").getValue().equals(cid));          
                  Iterator studentiterator = classElement.elementIterator("student");
                  while(studentiterator.hasNext())
                  {
                      Element studentElement=(Element)studentiterator.next();
                      if(studentElement.elementText("name").equals(name))
                      {
                          Element sexElement=studentElement.element("sex");
                          sexElement.setText(sex);
                          Element ageElement=studentElement.element("age");
                          ageElement.setText(age);
                          Element gradeElement=studentElement.element("grade");
                          gradeElement.setText(grade);       
                          altered=true;
                       }                                   
                  }      
               }    
           }
           
           try{
               /** 格式化输出,类型IE浏览一样 */
               OutputFormat format = OutputFormat.createPrettyPrint();
               /** 指定XML编码 */
               format.setEncoding("GBK");
              /** 将document中的内容写入文件中 */
//              XMLWriter writer = new XMLWriter(new FileWriter(new File(newfilename)),format);
               //保证编码为UTF-8,支持中文写入
              XMLWriter writer = new XMLWriter(new FileOutputStream(new File("manage.xml")),format);
              writer.write(document);
              writer.close();
              
           }catch(Exception ex){
              ex.printStackTrace();
           }
           }catch(Exception ex)
           {
               ex.printStackTrace();
            }     
    return altered;
}



/*
* 添加一个学生的信息
*/
public boolean add(String aid,String cid,String name,String sex,String age,String grade)
{
        boolean add = false;
        try {
            /*
            aid=new String(aid.getBytes("ISO8859-1"),"UTF-8");
            cid=new String(cid.getBytes("ISO8859-1"),"UTF-8");
            name=new String(name.getBytes("ISO8859-1"),"UTF-8");
            sex=new String(sex.getBytes("ISO8859-1"),"UTF-8");
            age=new String(age.getBytes("ISO8859-1"),"UTF-8");
            grade=new String(grade.getBytes("ISO8859-1"),"UTF-8");
            */
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(new java.io.File("manage.xml"));
            List academylist = document.selectNodes("/school/academy");
            Iterator academyiterator = academylist.iterator();
            while (academyiterator.hasNext()) 
            {
                Element academyElement = (Element) academyiterator.next();
                if (academyElement.attribute("aid").getValue().equals(aid)) 
                {
                    Iterator classiterator = academyElement.elementIterator("class");
                    while (classiterator.hasNext()) 
                    {
                        Element classElement = (Element) classiterator.next();
                        if (classElement.attribute("cid").getValue().equals(cid)) 
                        {
                            Element studentElement = classElement.addElement("student");
                            Element nameElement = studentElement.addElement("name");
                            nameElement.setText(name);
                            Element sexElement = studentElement.addElement("sex");
                            sexElement.setText(sex);
                            Element ageElement = studentElement.addElement("age");
                            ageElement.setText(age);
                            Element gradeElement = studentElement.addElement("grade");
                            gradeElement.setText(grade);
                            add = true;
                        }
                    }
                }
                
        }        
            try{
                   /** 格式化输出,类型IE浏览一样 */
                   OutputFormat format = OutputFormat.createPrettyPrint();
                   /** 指定XML编码 */
                   format.setEncoding("GBK");
                  /** 将document中的内容写入文件中 */
//                  XMLWriter writer = new XMLWriter(new FileWriter(new File(newfilename)),format);
                   //保证编码为UTF-8,支持中文写入
                  XMLWriter writer = new XMLWriter(new FileOutputStream(new File("manage.xml")),format);
                  writer.write(document);
                  writer.close();
                  
               }catch(Exception ex){
                  ex.printStackTrace();
               }
               
           }catch(Exception ex)
           {
                  ex.printStackTrace();
              }     
    return add;
}

}

 

alterframe   deleteframe  mainframe   queryframe等类则实现相应的界面显示。

 

相应xml结构如下:

六:总结:

   1 功能简单,操作不够灵活

2 在对数据的读入与存储上有待改进。

 

posted on 2013-03-03 23:07  烤德  阅读(1591)  评论(4编辑  收藏  举报