码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA对XML文件的操作( dom4j jaxen)

时间:2021-02-25 12:13:47      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:zha   username   puts   close   lxml   junit测试   cts   lte   public   

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);
    }
}

JAVA对XML文件的操作( dom4j jaxen)

标签:zha   username   puts   close   lxml   junit测试   cts   lte   public   

原文地址:https://www.cnblogs.com/zxingwork/p/14444550.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!