dom4j实现读入写出xml

来源:互联网 发布:物联网农业数据平台 编辑:程序博客网 时间:2024/06/05 14:18

一定要导入dom4j-1.6.1.jar包,然后再进行代码编写,这里是一个管理员注册的功能:

register.java:


package StuManage;
import java.io.File;
import java.io.FileOutputStream;

import java.util.List;
import java.util.Scanner;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class register {
    static String username;//用户名
    static String userphone;//电话号码
    static String useraccount;//账号
    static String userpwd;//密码
    
    static File f=new File("src/xml/manager.xml");
     //解析器    
    static SAXReader sr=new SAXReader();
    
    static Element e=null;
    
    //文档
    static Document d=null;
    
    //扫描器,扫描用户输入
    static Scanner sc=new Scanner(System.in);
    
    /**
     * 输入用户名
     */
    public static void uname(){
        System.out.println("请输入管理员名字:");
        username=sc.next();        
        if(!isuname()){
            System.out.println("管理员名字长度必须在2-12之间");
            uname();
        }else{
            uphone();
        }
    }
    
    /**
     * 判断管理员名长度
     * @return
     */
    public static boolean isuname(){
        if(username.length()>2&&username.length()<12){
            return true;
        }
        return false;
    }
    
    
    /**
     * 输入电话号码
     */
    public static void uphone(){
        System.out.println("请输入管理员电话号码:");
        userphone=sc.next();    
        if(!isuphone()){
            System.out.println("电话号码必须为11位,并且必须为1开头的数字");
            uphone();
        }
        else{
            uaccount();
        }
    }
    
    /**
     * 判断电话号码,长度,是否为数字
     * @return
     */
    public static boolean isuphone(){
        //累计数字有多少个
        int a=0;
        
        //循环取出电话号码的每个数
        for(int i=0;i<userphone.length();i++){
            
            //判断这个数是不是0-9之间的数字,判断的时候一定要加单引号。。。切记。。。
            if(userphone.charAt(i)>='0'&&userphone.charAt(i)<='9'){
                //判断第一个数是否为1
                if(userphone.charAt(0)=='1'){
                    a++;
                }
            }
        }
        //当长度 不为11或者长度与累计的a不相等时
        if(userphone.length()!=11||userphone.length()!=a){
            return false;
        }
        return true;
    }
    
    
    /**
     * 输入账号
     */
    public static void uaccount(){
        System.out.println("请输入管理员账号:");
        useraccount=sc.next();
        if(!isuaccount()){
            System.out.println("账号长度必须在2-12之间");
            uaccount();
        }
        else{
            if(f.exists()){
            try {
                //把xml读到Document里
                d=sr.read(f);
                
                //得到根节点
                e=d.getRootElement();
                
                List<Element> list=e.elements();//获取当前元素下的子元素
                
                //把子元素循环出来
                for(Element e1: list) {
                    if(e1.element("useraccount").getText().equals(useraccount)){
                        System.out.println("账号重名,请重新输入...");
                        uname();
                        break;
                    }
                }
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
            upwd();
        }
    }
    
    
    /**
     * 判断账号长度
     * @return
     */
    public static boolean isuaccount(){
        if(useraccount.length()>2&&userphone.length()<12){
            return true;
        }
        return false;
    }
    
    /**
     * 输入密码
     */
    public static void upwd(){
        System.out.println("请输入管理员密码:");
        userpwd=sc.next();
        if(!isupwd()){
            System.out.println("密码长度必须在6-12之间");
            upwd();
        }
        else{
            //给根元素添加子节点
            Element e1=e.addElement("user");
            
            //给子节点添加元素和设置文本
            e1.addElement("username").setText(username);
            e1.addElement("userphone").setText(userphone);    
            e1.addElement("useraccount").setText(useraccount);
            e1.addElement("userpwd").setText(userpwd);
            
            //创建解析对象
            XMLWriter xw=null;
            
            //设置编码格式
            OutputFormat ft=OutputFormat.createPrettyPrint();
            ft.setEncoding("GBK");
            
            try {
                xw=new XMLWriter(new FileOutputStream(f),ft);
                
                xw.write(d);
                xw.flush();
                xw.close();
            } catch (Exception e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }
            
            System.out.println("注册成功!");
        }
    }
    
    /**
     * 判断密码,长度
     * @return
     */
    public static boolean isupwd(){
        if(userpwd.length()>=6&&userpwd.length()<12){
            return true;
        }
        return false;
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("请选择:1,注册\t  2.登录  \t   3,添加 \t   4,修改\t  5,删除");
        int op=sc.nextInt();
        
        if(op==1){
            uname();
        }
    }

}


manager.xml:


<?xml version="1.0" encoding="GBK"?>

<users>
  <user>
    <username>张三</username>  
    <userphone>11011111111</userphone>  
    <useraccount>zhangsan</useraccount>  
    <userpwd>111111</userpwd>
  </user>  
  <user>
    <username>李四</username>  
    <userphone>11011010011</userphone>  
    <useraccount>001</useraccount>  
    <userpwd>111111</userpwd>
  </user>  
  <user>
    <username>123456</username>  
    <userphone>12345678909</userphone>  
    <useraccount>002</useraccount>  
    <userpwd>123456</userpwd>
  </user>  
  <user>
    <username>123</username>
    <userphone>18765432121</userphone>
    <useraccount>003</useraccount>
    <userpwd>123456</userpwd>
  </user>
</users>




0 0
原创粉丝点击