<?xml version="1.0" encoding="UTF-8"?>
<TUser>
</TUser>
我们要做的是对这个xml文件进行增删改操作:
共有六个类:
Computer.java 系统开始
Update.java 更新用户的信息
user.xml文件是放在F盘目录下哈;更改后是在F盘目录下产生出users.xml
@H_403_218@import java.util.Scanner;
@H_403_218@
@H_403_218@
@H_403_218@public class Computer {
@H_403_218@public Computer(){
@H_403_218@Scanner scan = new Scanner(System.in);
@H_403_218@int choice;
@H_403_218@while(true){
@H_403_218@choice = scan.nextInt();
@H_403_218@if(choice!=1&&choice!=2){
@H_403_218@}else{
@H_403_218@//如果输入的正确,则启动!
@H_403_218@if(choice==1){
@H_403_218@this.startXML();
@H_403_218@}else{
@H_403_218@}
@H_403_218@break;
@H_403_218@}
@H_403_218@}catch(Exception e){
@H_403_218@}
@H_403_218@}
@H_403_218@}
@H_403_218@public void startXML(){
@H_403_218@System.out.println("欢迎使用该系统!");
@H_403_218@Scanner scan = new Scanner(System.in);
@H_403_218@int num = 0;
@H_403_218@while(true){
@H_403_218@System.out.println("请输入要执行的操作!");
@H_403_218@try{
@H_403_218@num = scan.nextInt();
@H_403_218@if(num<1||num>4){
@H_403_218@continue;
@H_403_218@}
@H_403_218@}catch(Exception e){
@H_403_218@}
@H_403_218@switch(num){
@H_403_218@}
@H_403_218@}
@H_403_218@}
@H_403_218@}
@H_403_218@public static void main(String[] args) {
@H_403_218@//启动系统
@H_403_218@Computer com = new Computer();
@H_403_218@}
@H_403_218@}
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Add {
public static void add(){
while(true){
try{
User user = new User();
System.out.println("0");
user.createUser();
Add.addUser(user);
break;
}catch(Exception e){
System.out.println("输入错误!请重新输入!");
}
}
}
public static void addUser(User user){
try{
//读取文件
}
catch(DocumentException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Delete {
public static void delete(){
Scanner scan = new Scanner(System.in);
String num = null;
while(true){
try{
num = scan.next();
HashMap<String,String> map = Query.startQuery(num);
Set set = map.keySet();
for(Iterator iter = set.iterator();iter.hasNext();){
String key = (String)iter.next();
String value = map.get(key);
System.out.println(key+":"+value);
}
//如果用户不存在,则不执行下面的操作,如果存在,则执行
if(map.size()>1){
Delete.toDelete(num);
}
break;
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void toDelete(String num){
SAXReader reader = new SAXReader();
try {
//得到user.xml的Document
Document doc = reader.read(new File("F://user.xml"));
//得到跟结点
Element root = doc.getRootElement();
//得到根节点下的所有user集合
List<Element> userList = doc.selectNodes("//TUser/user");
//得到迭代器
Iterator<Element> iter = userList.iterator();
while(iter.hasNext()){
//得到user元素
Element e = iter.next();
//得到这个user元素的userID
Attribute att = e.attribute("userID");
//判断这个userID是不是我们想要的user
if(num.equals(att.getText())){
//如果是,则把自己(e)从跟结点处删除
root.remove(e);
}
}
//得到XML的输出流
XMLWriter output;
try {
output = new XMLWriter(new FileWriter( new File("F://users.xml") ));
//将内存中的doc写入到新文件当中
output.write(doc);
output.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Query {
public static void getQuery(){
//定义输入
Scanner scan = new Scanner(System.in);
String num = null;
while(true){
try{
num = scan.next();
HashMap<String,String> map = startQuery(num);
Set set = map.keySet();
for(Iterator iter = set.iterator();iter.hasNext();){
String key = (String)iter.next();
String value = map.get(key);
System.out.println(key+":"+value);
}
break;
}catch(Exception e){
System.out.println("输入错误!请重新输入!");
}
}
}
public static HashMap<String,String> startQuery(String num){
SAXReader reader = new SAXReader();
HashMap<String,String> map = new HashMap<String,String>();
try {
//得到user.xml的这个doc
Document doc = reader.read(new File("F://user.xml"));
//得到所有用户的一个集合userList
List<Element> userList = doc.selectNodes("//TUser/user");
//得到迭代器,
Iterator<Element> iter = userList.iterator();
while(iter.hasNext()){
//得到每一个user元素,进行判断
Element e = iter.next();
//得到这个user的ID
Attribute att = e.attribute("userID");
//如果这个user的ID和我们查询的ID一致,则将其所有的信息放入到map当中
if(num.equals(att.getText())){
//得到这个user的所有子元素
List<Element> childElements = e.elements();
//得到所有子元素的迭代器
Iterator<Element> childIter = childElements.iterator();
//如果还有子元素,则
while(childIter.hasNext()){
//得到子元素,将其信息放入到map当中
Element childElement = childIter.next();
map.put(childElement.getQName().getName(),childElement.getText());
}
}
}
}catch(Exception e){
System.out.println("读取城市失败!");
e.printStackTrace();
}
//如果map为0,则说明没有查找到我们要的信息,则往map当中添加未找到信息
if(map.size()<1){
map.put("您查找的信息不存在","");
}
return map;
}
}
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Update {
public static void update(){
Scanner scan = new Scanner(System.in);
String num = null;
Scanner scan1 = new Scanner(System.in);
String element = null;
Scanner scan2 = new Scanner(System.in);
String elementValue = null;
while(true){
try{
num = scan.next();
HashMap<String,String> map = Query.startQuery(num);
Set set = map.keySet();
for(Iterator iter = set.iterator();iter.hasNext();){
String key = (String)iter.next();
String value = map.get(key);
System.out.println(key+":"+value);
}
//如果用户不存在,则不执行下面的操作,如果存在,则执行
if(map.size()>1){
System.out.println("请输入你要修改的元素:");
element = scan1.next();
System.out.println("请输入你要修改元素的值:");
elementValue = scan2.next();
Update.updateUser(num,element,elementValue);
}
break;
}catch(Exception e){
System.out.println("输入错误!请重新输入!");
}
}
}
public static void updateUser(String num,String element,String value){
try {
//得到user.xml文件,将其读取到doc当中
SAXReader reader = new SAXReader();
Document doc = reader.read(new File("F://user.xml"));
//得到跟结点root
Element root = doc.getRootElement();
//得到根节点TUser下的所有用户,将其存放到userList当中
List<Element> userList = doc.selectNodes("//TUser/user");
//得到可以访问所有用户的迭代器Iterator
Iterator<Element> iter = userList.iterator();
//如果迭代器还有下一个,则取出来
while(iter.hasNext()){
//得到迭代器中下一个元素
Element user = iter.next();
//得到user元素的一个属性,即userID,
Attribute userId = user.attribute("userID");
if(num.equals(userId.getText())){
//通过user.element("子元素名")得到我们要修改的子元素
Element childElement = user.element(element);
//修改子元素的text值
childElement.setText(value);
}
}
//得到XML的输出流
XMLWriter output;
try {
output = new XMLWriter(new FileWriter( new File("F://users.xml") ));
//将内存中的doc写入到新文件当中
output.write(doc);
output.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
import java.util.Date;
import java.util.Scanner;
public class User {
private String userID;
private String UserName;
private String DeptId;
private String Sex;
private String IconId;
private String City;
private String Password;
private String OnLineState;
private String LastLoginTime;
private String RegisterTime;
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getDeptId() {
return DeptId;
}
public void setDeptId(String deptId) {
DeptId = deptId;
}
public String getSex() {
return Sex;
}
public void setSex(String sex) {
Sex = sex;
}
public String getIconId() {
return IconId;
}
public void setIconId(String iconId) {
IconId = iconId;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
public String getOnLineState() {
return OnLineState;
}
public void setOnLineState(String onLineState) {
OnLineState = onLineState;
}
public String getLastLoginTime() {
return LastLoginTime;
}
public void setLastLoginTime(String lastLoginTime) {
LastLoginTime = lastLoginTime;
}
public String getRegisterTime() {
return RegisterTime;
}
public void setRegisterTime(String registerTime) {
RegisterTime = registerTime;
}
public void createUser(){
Scanner scan = new Scanner(System.in);
String message = null;
System.out.println("请输入userID");
message = scan.next();
this.setUserID(message);
System.out.println("请输入UserName");
message = scan.next();
this.setUserName(message);
System.out.println("请输入DeptId");
message = scan.next();
this.setDeptId(message);
System.out.println("请输入Sex");
message = scan.next();
this.setSex(message);
System.out.println("请输入IconId");
message = scan.next();
this.setIconId(message);
System.out.println("请输入City");
message = scan.next();
this.setCity(message);
System.out.println("请输入Password");
message = scan.next();
this.setPassword(message);
System.out.println("请输入OnLineState");
message = scan.next();
this.setOnLineState(message);
this.setLastLoginTime(new Date().toLocaleString());
this.setRegisterTime(new Date().toLocaleString());
}
}
这是修改后的xml,City从福州变为德化