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);
        }
    }
    
  • 相关阅读:
    5-python基础—获取某个目录下的文件列表(适用于任何系统)
    Automated, Self-Service Provisioning of VMs Using HyperForm (Part 1) (使用HyperForm自动配置虚拟机(第1部分)
    CloudStack Support in Apache libcloud(Apache libcloud中对CloudStack支持)
    Deploying MicroProfile-Based Java Apps to Bluemix(将基于MicroProfile的Java应用程序部署到Bluemix)
    Adding Persistent Storage to Red Hat CDK Kit 3.0 (在Red Hat CDK Kit 3.0添加永久性存储)
    Carve Your Laptop Into VMs Using Vagrant(使用Vagran把您笔记本电脑刻录成虚拟机)
    使用Python生成一张用于登陆验证的字符图片
    Jupyter notebook的安装方法
    Ubuntu16.04使用Anaconda5搭建TensorFlow使用环境 图文详细教程
    不同时区的换算
  • 原文地址:https://www.cnblogs.com/zxingwork/p/14444550.html
Copyright © 2011-2022 走看看