XML 实例

package cn.xml;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Iterator;

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

/**
 * 解析animal xml文件,对xml查询,增加、修改、删除
 * @author Administrator
 *
 */
public class AnimalDao extends BaseDao{
	//查询所有节点内容
	public void query() throws DocumentException {
		//先得到根节点
		Element root=doc.getRootElement();
		//遍历根元素的所有子节点
		Iterator es=root.elementIterator();
		System.out.println("名称|\t类型|\t颜色|\t年龄|\t性别|");
		while(es.hasNext()){
			Element animal=(Element)es.next();
			Iterator its=animal.elementIterator();
			
			while(its.hasNext()){
				//得到每个animal的所有子节点
				Element el=(Element)its.next();
				String str=el.getText();//得到子节点的文本
				System.out.print(str+"|\t");
			}
			System.out.println();
		}
	}
	public void query2() throws DocumentException{
		//得到根节点
		Element root=doc.getRootElement();
		//得到根节点的所有子节点animal
		Iterator animals=root.elementIterator();
		System.out.println("id\t名称|\t类型|\t颜色|\t年龄|\t性别|");
		while(animals.hasNext()){
			Element animal=(Element)animals.next();
			//得到animal属性id的值
			String id=animal.attributeValue("id");
			String name=animal.element("name").getText();
			String type=animal.elementText("type");
			String color=animal.elementText("color");
			String age=animal.elementText("age");
			String sex=animal.elementText("sex");
			System.out.println(id+"\t"+name+"\t"+type+"\t"+color+"\t"+age+"\t"+sex);
		}
	}
	//根据编号查询id=01
	//增加动物对象
	public void add(String id,String name,String type,String color,String age,String sex){
		//给root添加子节点animal
		Element root=doc.getRootElement();
		//创建animal子节点
		Element animal=root.addElement("animal");
		//添加属性
		animal.addAttribute("id", id);
		//添加animal里的子节点name,type,color,age,sex
		animal.addElement("name").addText(name);
		animal.addElement("type").addText(type);
		animal.addElement("color").addText(color);
		animal.addElement("age").addText(age);
		animal.addElement("sex").addText(sex);
	}
	//修改动物对象,通过编号修改
	public void modify(String id,String name,String type,String color,String age,String sex){
		Element root=doc.getRootElement();
		Iterator it=root.elementIterator();
		boolean tag=false;
		while(it.hasNext()){
			Element animal=(Element)it.next();
			String aid=animal.attributeValue("id");
			if(aid.equals(id)){
				tag=true;
				//找到要修改的编号
				animal.element("name").setText(name);
				animal.element("type").setText(type);
				animal.element("color").setText(color);
				animal.element("age").setText(age);
				animal.element("sex").setText(sex);
			}
		}
		save();//将修改的内容保存到xml文档
		if(!tag){
			System.out.println("没有此编号");
		}
	}
	//删除动物信息
	public void del(String id){
		Element root=doc.getRootElement();
		Iterator it=root.elementIterator();
		boolean tag=false;
		while(it.hasNext()){
			Element animal=(Element)it.next();
			String aid=animal.attributeValue("id");
			if(aid.equals(id)){
				tag=true;
				if(root.remove(animal)){
					System.out.println("删除成功");
					break;
				}
			}
		}
		save();
		if(tag==false){
			System.out.println("没有此对象");
		}
	}
	
}

 

package cn.xml;

import java.util.Scanner;

import org.dom4j.DocumentException;
/**
 * 动物管理系统
 * @author Administrator
 *
 */
public class AnimalManager {
	AnimalDao ad=new AnimalDao();
	Scanner input=new Scanner(System.in);
	public void start(){
		try {
			ad.parse("xml/animals.xml");
		} catch (DocumentException e) {
			System.out.println("文件找不到");
		}
		do{
			System.out.println("---------欢迎使用动物后台管理系统-------------");
			System.out.println("1.查询所有动物信息 2.增加  3.修改 4.删除 5.退出");
			int option=input.nextInt();
			switch (option) {
			case 1:
				try {
					ad.query();
				} catch (DocumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			case 2:
				System.out.println("请输入动物编号:");
				String id=input.next();
				System.out.println("请输入动物姓名:");
				String name=input.next();
				System.out.println("请输入动物品种:");
				String type=input.next();
				System.out.println("请输入动物颜色:");
				String color=input.next();
				System.out.println("请输入动物年龄:");
				String age=input.next();
				System.out.println("请输入动物性别:");
				String sex=input.next();
				ad.add(id,name, type, color, age, sex);
				ad.save();
				break;
			case 3:
				System.out.println("请输入动物编号:");
				id=input.next();
				System.out.println("请输入动物姓名:");
				name=input.next();
				System.out.println("请输入动物品种:");
				type=input.next();
				System.out.println("请输入动物颜色:");
				color=input.next();
				System.out.println("请输入动物年龄:");
				age=input.next();
				System.out.println("请输入动物性别:");
				sex=input.next();
				ad.modify(id, name, type, color, age, sex);
				break;
			case 4:
				System.out.println("请输入动物编号:");
				id=input.next();
				ad.del(id);
				break;
			case 5:
				System.out.println("程序结束!");
				System.exit(0);
			default:
				
				break;
			}
		}while(true);
	}
	
}

 

 

package cn.xml;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

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

public class BaseDao {
	protected Document doc;
	//加载整个Document文档
	public void parse(String url) throws DocumentException {
        SAXReader reader = new SAXReader();
        doc = reader.read(url);
    }
	//保存xml文档
	public void save(){
		//FileWriter out=null;

		//乱码,且不能设置字符编码
		/*out = new FileWriter("xml/animals.xml");
		doc.write(out);*/
		XMLWriter writer=null;
		OutputFormat format=null;
		try {
			format = OutputFormat.createPrettyPrint();
			writer = new XMLWriter(new OutputStreamWriter(
					new FileOutputStream("xml/animals.xml"),
					"utf-8"),format);
			writer.write( doc );
			writer.flush();
			System.out.println("保存成功");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		}
}

 

package cn.xml;

import java.util.Scanner;

import org.dom4j.Document;
import org.dom4j.DocumentException;

public class Test {
	public static void main(String[] args) {
		UsersDao ud=new UsersDao();
		Scanner input=new Scanner(System.in);
		do{
			System.out.println("请输入用户名:");
			String name=input.next();
			System.out.println("请输入密码:");
			String pwd=input.next();
			String str=ud.getRole(name, pwd);
			if(str.length()>0){
				//登录成功
				if(str.equals("manager")){//管理员
					AnimalManager am=new AnimalManager();
					am.start();
				}
				else{
					//调用收银系统
				}
			}
			else{//用户名密码错误
				System.out.println("用户名密码错误,请重新输入");
			}
		}while(true);
	}
}

 

package cn.xml;

import java.util.Iterator;

import org.dom4j.DocumentException;
import org.dom4j.Element;

public class UsersDao extends BaseDao{
	//根据用户名密码查询角色
	public String getRole(String name,String pwd){
		try {
			super.parse("xml/users.xml");
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//遍历所有子节点
		Element root=doc.getRootElement();
		Iterator it= root.elementIterator();
		while(it.hasNext()){
			Element user=(Element)it.next();
			String uname=user.elementText("name");
			String password=user.elementText("pwd");
			if(uname.equals(name)&&password.equals(pwd)){
				return user.elementText("role");
			}
		}
		//没找到,用户名密码错误
		return "";
	}
}

 

你可能感兴趣的:(悲催笔记OTL..)