domj4的增删改查保存<带介绍>

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

游迹AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值