zoukankan      html  css  js  c++  java
  • JAVA对XML文件的操作( dom4j jaxen)

    XML文件读取和写入

    package com.example.mvcdemo;
    
    import org.dom4j.*;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.SAXReader;
    import org.dom4j.io.XMLWriter;
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.List;
    
    public class UserImplXML {
        private String path = "user.xml";
    
    //  读取xml文件和解析
        public User find(String username, String password){
    //        InputStream inputStream = UserImplXML.class.getClassLoader().getResourceAsStream("user.xml");
    
            SAXReader saxReader = new SAXReader();
    
    
            try {
                Document document = saxReader.read(path);
    
    //            Element element = (Element) document.getRootElement();
                String xpath = "/users/user[@username='zhangxing' and @password='1234']";
                Element element = (Element) document.selectSingleNode(xpath);
                if (element==null){
                    System.out.println("element is null");
                    return null;
                }
                User user = new User();
                user.setId(Integer.parseInt(element.attributeValue("id")));
                user.setUsername(element.attributeValue("username"));
                user.setPassword(element.attributeValue("password"));
                user.setEmail(element.attributeValue("email"));
    
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date birthday = simpleDateFormat.parse(element.attributeValue("birthday"));
                user.setBirthday(birthday);
                return user;
    
            } catch (DocumentException e) {
                e.printStackTrace();
                throw new RuntimeException("初始化出错");
            }catch (ParseException e){
                e.printStackTrace();
                throw new RuntimeException("查询时出错");
            }
        }
    
    //  写入xml文件
        public void register(User user) throws DocumentException, IOException {
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(path);
    
            Element newElement = DocumentHelper.createElement("user");
            newElement.addAttribute("id", String.valueOf(user.getId()));
            newElement.addAttribute("username", user.getUsername());
            newElement.addAttribute("password", user.getPassword());
            newElement.addAttribute("email", user.getEmail());
    
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String birthday = simpleDateFormat.format(user.getBirthday());
            newElement.addAttribute("birthday", birthday);
            document.getRootElement().add(newElement);
    
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            XMLWriter xmlWriter = new XMLWriter(new FileWriter(path), outputFormat);
            xmlWriter.write(document);
            xmlWriter.close();
        }
    }
    
    

    Junit测试代码

    package com.example.mvcdemo;
    
    import org.dom4j.DocumentException;
    import org.junit.jupiter.api.Test;
    
    import java.io.IOException;
    import java.util.Date;
    
    import static org.junit.jupiter.api.Assertions.*;
    
    class UserImplXMLTest {
        private String username = "zhongfucheng";
        private String password = "123";
        @Test
        void find() {
            UserImplXML userImplXML = new UserImplXML();
            User user = userImplXML.find(username, password);
            System.out.println(user);
    
            System.out.println("birthday:"+user.getBirthday());
            System.out.println("email:"+user.getEmail());
            System.out.println("id:"+user.getId());
            System.out.println("username:"+user.getUsername());
            System.out.println("password:"+user.getPassword());
        }
    
        @Test
        void register() throws IOException, DocumentException {
            UserImplXML userImplXML = new UserImplXML();
            User user = new User(3, "zx3", "122", "zx@163.com", new Date());
            userImplXML.register(user);
        }
    }
    
  • 相关阅读:
    居中
    redis之列表操作及在django中操作等相关内容-124
    模型类序列化器ModelSerializer的使用等相关内容-84
    序列化类Serializer的基础使用等相关内容-83
    redis的基础使用等相关内容-123
    django框架前后端混合项目之子评论、后台管理页面富文本编辑器、修改头像、修改密码等相关内容-81
    vue框架前后端分离项目之短信验证码、登录、注册接口及前端登陆注册功能等相关内容-122
    web开发模式及drf的介绍等相关内容-82
    django框架前后端混合项目之侧边栏及点赞点踩功能等相关内容-80
    django框架前后端混合项目之首页轮播图、主页、后台管理、头像显示等相关内容-79
  • 原文地址:https://www.cnblogs.com/zxingwork/p/14444550.html
Copyright © 2011-2022 走看看