package dom增删改查;
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;
import java.io.*;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class DomZSGC {
static List<String> swa =new ArrayList();
static File file=new File("src/powxm.xml");
static {
swa.add("name");
swa.add("college");
swa.add("teleohone");
swa.add("notes");
}
public static void main(String[] args) throws MalformedURLException, DocumentException {
SAXReader read=new SAXReader(); //获取工厂
Document document = read.read(file); //解析xml
Element root = document.getRootElement(); //根节点
Zsgc zsgc=new Zsgc(swa,file);
Scanner sc=new Scanner(System.in);
while(true){
switch (sc.nextInt()){
case 1:zsgc.add(root); //添加
break;
case 2: zsgc.element(root); //修改
break;
case 3:zsgc.remove(root); //删除
break;
case 4: zsgc.query(root); //查询
break;
case 5: zsgc.save(document); //保存
break;
default:System.exit(0); //结束
}
}
}
}
class Zsgc{
List<String> swa;
File file;
String str1[]={"张三","信息学院","1002","你好"};
int sum;
Scanner sc=new Scanner(System.in);
public Zsgc(List<String> swa, File file) {
this.swa = swa;
this.file = file;
}
void add(Element root) { //添加
Element stu = root.addElement("studnet");//添加根节点
for(String swa:swa){
Element name = stu.addElement(swa); //设置标签
name.setText(str1[sum++]); //设置姓名
}
sum=0;
System.out.println("添加成功");
}
void remove(Element root) { //删除
System.out.println("请输入需要删除人的姓名:");
int zhao = zhao(root, sc.next());//查找id
Element studnemt = (Element) root.elements("studnet").get(zhao);//获取第1个根节点
root.remove(studnemt);//移除
System.out.println("删除成功");
}
void element(Element root) { //修改 //doc 解析xml的 root 根节点
System.out.println("请输入需要修改人的姓名:");
int zhao = zhao(root, sc.next());//查找id
System.out.println(zhao);
Element studnet = (Element) root.elements("studnet").get(zhao);//获取第1个根节点
for(String s1:swa)
{
System.out.println("请输入"+s1+":");
studnet.element(s1).setText(sc.next());
}
}
void query(Element root ) { //查
System.out.println();
// 遍历根节点
for (Iterator<Element> rootie = root.elementIterator(); rootie.hasNext();) {
Element next = rootie.next(); //获取当前节点<第一层>
for(Iterator<Element> nextItre=next.elementIterator();nextItre.hasNext();){ //获取第二成节点
Element next1 = nextItre.next();
String value = next1.getStringValue(); //获取所对应的值
System.out.println(next1.getName()+":"+value);
}
sum=0;
System.out.println();
}
}
public void save(Document doc) { //保存
try{
OutputFormat out=new OutputFormat().createPrettyPrint(); //读
out.setEncoding("utf-8");//转码
OutputStream stream=new FileOutputStream(file);//写入
XMLWriter xml=new XMLWriter(stream,out);//写
xml.write(doc);
stream.close();
System.out.println("保存成功");
}catch (Exception e){
System.out.println("保存失败");
}
}
public int zhao(Element root,String str) { //找
int sum1=0;
// 遍历根节点
for (Iterator<Element> rootie = root.elementIterator(); rootie.hasNext();) {
Iterator<Element> nextItre=rootie.next().elementIterator(); //下一个节点
String value = nextItre.next().getStringValue();
if(str.equals(value)){//比较是不是对应的
break;
}
sum1++;
}
return sum1; //输出在第几个
}
}
所用的xml:
<?xml version="1.0" encoding="utf-8"?>
<Students>
<studnet>
<name>q1</name>
<college>信息学院</college>
<teleohone>1002</teleohone>
<notes>你好</notes>
</studnet>
<studnet>
<name>q2</name>
<college>信息学院</college>
<teleohone>1002</teleohone>
<notes>你好</notes>
</studnet>
<studnet>
<name>q3</name>
<college>信息学院</college>
<teleohone>1002</teleohone>
<notes>你好</notes>
</studnet>
<studnet>
<name>q4</name>
<college>信息学院</college>
<teleohone>1002</teleohone>
<notes>你好</notes>
</studnet>
</Students>