XML文档的创建及基本操作

创建一个XML文件,向其中添加Student类型的元素,再对其中的元素进行操作。
Student类
package com.xml;
//学生类
public class Student {
    String name;
    String sex;
    String school;
    public Student(String name,String sex,String school){
        this.name=name;
        this.sex=sex;
        this.school=school;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getSchool() {
        return school;
    }
    public void setSchool(String school) {
        this.school = school;
    }
}
操作类
package com.xml;
import java.io.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import java.util.*;
/**XML的解析
 * 对XML文件进行增删改查操作
 * @author Administrator
 *
 */
public class Test {
    //创建XML
    public static void creatXml() throws FileNotFoundException, IOException{
        //创建文档
        Document d=new Document();
        //创建根节点
        Element e=new Element("Student");
        //添加子节点元素
        e.addContent(new Element("Student")
         .addContent(new Element("name").setText("张三"))
         .addContent(new Element("sex").setText("男"))
         .addContent(new Element("school").setText("桂工")));
        //将子节点元素追加到根节点
        d.addContent(e);
        XMLOutputter o=new XMLOutputter(Format.getPrettyFormat().setEncoding("utf-8").setIndent(" "));
        o.output(d, new FileOutputStream(new File("student.xml")));
    }

    public static void addXML(Student s)throws Exception{
        //获取XML文件
        SAXBuilder b=new SAXBuilder();
        Document doc=b.build(new File("student.xml"));
        //获取根节点
        Element e=doc.getRootElement();
        //向已存在的xml中添加元素
        e.addContent(new Element("student")
          .addContent(new Element("name").setText(s.getName()))
          .addContent(new Element("sex").setText(s.getSex()))
          .addContent(new Element("school").setText(s.getSchool()))
        );
        System.out.println("添加成功");
        //doc.addContent(e);
        XMLOutputter o=new XMLOutputter(Format.getPrettyFormat().setEncoding("utf-8").setIndent(" "));
        o.output(doc, new FileOutputStream(new File("student.xml")));
    }
    //修改元素
    public static void updateXml(String oldName,String newName)throws Exception{
        SAXBuilder b=new SAXBuilder();
        Document doc=b.build(new File("student.xml"));
        Element e=doc.getRootElement();
        List<Element> list=e.getChildren();
        for (int i = 0; i < list.size(); i++) {
            Element ee=list.get(i);
            if(ee.getChildText("name").equals(oldName)){
                ee.getChild("name").setText(newName);
                System.out.println("修改成功");
            }
        }
        XMLOutputter o=new XMLOutputter(Format.getPrettyFormat().setEncoding("utf-8").setIndent(" "));
        o.output(doc, new FileOutputStream(new File("student.xml")));
    }

    //删除元素
    public static void delXml(String name) throws JDOMException, IOException{
        SAXBuilder b=new SAXBuilder();
        Document doc=b.build(new File("student.xml"));
        Element e=doc.getRootElement();
        List<Element> list=e.getChildren();
        for (int i = 0; i < list.size(); i++) {
            Element ee=list.get(i);
            if(ee.getChildText("name").equals(name)){
                e.removeContent(ee);
                System.out.println("删除成功");
            }
        }
        XMLOutputter o=new XMLOutputter(Format.getPrettyFormat().setEncoding("utf-8").setIndent(" "));
        o.output(doc, new FileOutputStream(new File("student.xml")));
    }

    //查找元素
    public static void findByName(String name) throws JDOMException, IOException{
        SAXBuilder b=new SAXBuilder();
        Document doc=b.build(new File("student.xml"));
        Element e=doc.getRootElement();
        List<Element> list=e.getChildren();
        for (int i = 0; i < list.size(); i++) {
            Element ee=list.get(i);
            if(ee.getChildText("name").equals(name)){
                System.out.println(ee.getChildText("name"));
                System.out.println(ee.getChildText("sex"));
                System.out.println(ee.getChildText("school"));
            }
        }
        XMLOutputter o=new XMLOutputter(Format.getPrettyFormat().setEncoding("utf-8").setIndent(" "));
        o.output(doc, new FileOutputStream(new File("student.xml")));
    }
    public static void main(String[] args) throws Exception{
        //delXml("张三");
        //Student s=new Student("小花","nv","清华");
        //addXML(s);
        //findByName("张三");
        //updateXml("小花", "小花花");
        findByName("小花花");
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值