XML的增删改查

前端之家收集整理的这篇文章主要介绍了XML的增删改查前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<h3 style="padding: 0px; margin: 0px 0px 2px; font-size: 10pt; color: rgb(0,102,0); font-family: 微软雅黑,Verdana,sans-serif,宋体;">XMLUtil.java文件</h3>
package XMLcrud;
 
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
 
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
 
public class XMLUtil {
    private static String fileURL="/user.xml";
     
    /**
     * 获取XML中所有的用户信息
     * @return
     */
    public static List<User> getAllUser(){
        Document document=getDocument();
        Element rootElement=document.getRootElement();
         
        List<Element> userElements=rootElement.elements();
        List<User> users=new ArrayList<User>();
        for (Element userElement : userElements) {
            User user=new User();
            user.setId(userElement.attributeValue("id"));
            user.setName(userElement.element("name").getTextTrim());
            user.setAge(userElement.element("age").getTextTrim());
            users.add(user);
        }
        return users;
    }
     
    /**
     * 向XML文件添加一个user
     * @param user
     */
    public static void add(User user){
        Document document=getDocument();
        Element rootElement=document.getRootElement();
         
        Element userElement=rootElement.addElement("user");
        userElement.addAttribute("id",getMaxId()+1+"");
        Element nameElement=userElement.addElement("name");
        Element ageElement=userElement.addElement("age");
        nameElement.setText(user.getName());
        ageElement.setText(user.getAge());
         
        write2XML(document);
    }
    /**
     * 根据id删除user
     * @param id
     */
    public static void deleteById(String id){
        Document document=getDocument();
        Element rootElement=document.getRootElement();
         
        List<Element> userElements=rootElement.elements("user");
        for (Element userElement : userElements) {
            if(userElement.attributeValue("id").equals(id)){
                System.out.println("开始删除.....");
                rootElement.remove(userElement);
                System.out.println("删除结束.....");
            }
        }
        write2XML(document);
    }
    /**
     * 修改user信息
     * @param user
     */
    public static void update(User user){
        Document document=getDocument();
        Element rootElement=document.getRootElement();
         
        List<Element> userElements=rootElement.elements();
        for (Element userElement : userElements) {
            if(userElement.attributeValue("id").equals(user.getId())){
                List<Element> elements=userElement.elements();
                userElement.element("name").setText(user.getName());
                userElement.element("age").setText(user.getAge());
            }
        }
        write2XML(document);
    }
     
    /**
     * 获取根节点
     * @return rootElement
     */
    public static Document getDocument(){
        try {
            SAXReader saxReader=new SAXReader();
            InputStream in=OperateXML.class.getResourceAsStream("/user.xml");
            Document document=saxReader.read(in);
             
            return document;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    /**
     * 把内容写到XML文件中
     * @param document
     */
    public static void write2XML(Document document){
        try {
            OutputFormat format=new OutputFormat("  ",true,"utf-8");
            format.setTrimText(true);
            XMLWriter writer = new XMLWriter(format);
            writer.setOutputStream(new FileOutputStream("src"+fileURL));
            writer.write(document);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    /**
     * 获取xml文件中userId的最大值
     * @return int
     */
    public static int getMaxId(){
        int maxId=0;
        Document document=getDocument();
        Element rootElement=document.getRootElement();
        List<Element> userElements=rootElement.elements();
        for (Element element : userElements) {
            int id=Integer.valueOf(element.attributeValue("id"));
            if(maxId<id){
                maxId=id;
            }
        }
        return maxId;
    }
}

User.java文件

package XMLcrud;
 
public class User {
    private String id;
    private String name;
    private String age;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
     
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ",name=" + name + ",age=" + age + "]";
    }
     
}

user.xml文件
<?xml version="1.0" encoding="utf-8"?>
 
<users>
  <user id="1">
    <name>张三</name>
    <age>18</age>
  </user>
  <user id="2">
    <name>李四</name>
    <age>10</age>
  </user>
</users>
原文链接:https://www.f2er.com/xml/297681.html

猜你在找的XML相关文章