码迷,mamicode.com
首页 > 数据库 > 详细

数据库:XML,解析Dom4J

时间:2018-01-28 11:24:31      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:tin   put   document   getc   null   stream   col   void   save   

package com.itheima.util;

import java.io.FileOutputStream;
import java.net.URL;

import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class Dom4JUtil {
    private static String xmlRealpath;
    static{
        ClassLoader cl = Dom4JUtil.class.getClassLoader();
        URL url = cl.getResource("users.xml");
        xmlRealpath = url.getPath();
    }
    
    public static Document getDocument() throws Exception{
        SAXReader reader = new SAXReader();
//        return reader.read("src/users.xml");//错误
        return reader.read(xmlRealpath);
    }
    public static void write2xml(Document document) throws Exception{
        XMLWriter writer = new XMLWriter(new FileOutputStream(xmlRealpath), OutputFormat.createPrettyPrint());
        writer.write(document);
        writer.close();
    }
}
package com.itheima.dao.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.util.Dom4JUtil;

public class UserDaoXmlImpl implements UserDao {

    public User findByUsername(String username) {
        try {
            Document doc = Dom4JUtil.getDocument();
//            List<Node> userNodes = doc.selectNodes("//user");
            Node node = doc.selectSingleNode("//user[@username=‘"+username+"‘]");
            if(node==null)
                return null;
            
            String xmlBirthday = node.valueOf("@birthday");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date birthday = df.parse(xmlBirthday);
            
            User user = new User(node.valueOf("@username"), node.valueOf("@password"), node.valueOf("@email"), birthday);
            return user;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    //<user username="abc" password="123" email="wzt@itcast.cn" birthday="1980-10-01"/>
    public void save(User user) {
        try {
            Document doc = Dom4JUtil.getDocument();
            Element root = doc.getRootElement();
            root.addElement("user")
                .addAttribute("username", user.getUsername())
                .addAttribute("password", user.getPassword())
                .addAttribute("email", user.getEmail())
                .addAttribute("birthday", user.getBirthday().toLocaleString());
            Dom4JUtil.write2xml(doc);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public User findUser(String username, String password) {
        try {
            Document doc = Dom4JUtil.getDocument();
//            List<Node> userNodes = doc.selectNodes("//user");
            Node node = doc.selectSingleNode("//user[@username=‘"+username+"‘ and @password=‘"+password+"‘]");
            if(node==null)
                return null;
            
            String xmlBirthday = node.valueOf("@birthday");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date birthday = df.parse(xmlBirthday);
            
            User user = new User(node.valueOf("@username"), node.valueOf("@password"), node.valueOf("@email"), birthday);
            return user;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
package com.itheima.dao.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.util.Dom4JUtil;

public class UserDaoXmlImpl implements UserDao {

    public User findByUsername(String username) {
        try {
            Document doc = Dom4JUtil.getDocument();
//            List<Node> userNodes = doc.selectNodes("//user");
            Node node = doc.selectSingleNode("//user[@username=‘"+username+"‘]");
            if(node==null)
                return null;
            
            String xmlBirthday = node.valueOf("@birthday");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date birthday = df.parse(xmlBirthday);
            
            User user = new User(node.valueOf("@username"), node.valueOf("@password"), node.valueOf("@email"), birthday);
            return user;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    //<user username="abc" password="123" email="wzt@itcast.cn" birthday="1980-10-01"/>
    public void save(User user) {
        try {
            Document doc = Dom4JUtil.getDocument();
            Element root = doc.getRootElement();
            root.addElement("user")
                .addAttribute("username", user.getUsername())
                .addAttribute("password", user.getPassword())
                .addAttribute("email", user.getEmail())
                .addAttribute("birthday", user.getBirthday().toLocaleString());
            Dom4JUtil.write2xml(doc);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public User findUser(String username, String password) {
        try {
            Document doc = Dom4JUtil.getDocument();
//            List<Node> userNodes = doc.selectNodes("//user");
            Node node = doc.selectSingleNode("//user[@username=‘"+username+"‘ and @password=‘"+password+"‘]");
            if(node==null)
                return null;
            
            String xmlBirthday = node.valueOf("@birthday");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date birthday = df.parse(xmlBirthday);
            
            User user = new User(node.valueOf("@username"), node.valueOf("@password"), node.valueOf("@email"), birthday);
            return user;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
package com.itheima.test;

import static org.junit.Assert.*;

import java.util.Date;

import org.junit.Test;

import com.itheima.domain.User;
import com.itheima.exception.UserHasExistException;
import com.itheima.service.BusinessService;
import com.itheima.service.impl.BusinessServiceImpl;

public class BusinessServiceImplTest {
    private BusinessService s = new BusinessServiceImpl();
    @Test
    public void testRegist() throws UserHasExistException {
        User user = new User("wzhting", "123", "wzt@itcast.cn", new Date());
        s.regist(user);
    }
    @Test(expected=com.itheima.exception.UserHasExistException.class)
    public void testRegist1() throws UserHasExistException {
        User user = new User("wzhting", "123", "wzt@itcast.cn", new Date());
        s.regist(user);
    }
    @Test
    public void testLogin() {
        User user = s.login("wzhting", "123");
        assertNotNull(user);
        user = s.login("wzhting", "111");
        assertNull(user);
        user = s.login("asf", "111");
        assertNull(user);
    }

}
package com.itheima.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;

public class BeanUtil {
    //把请求参数封装到指定的JavaBean中
    public static <T> T fillBean(HttpServletRequest request,Class<T> clazz){
        try {
            T bean = clazz.newInstance();
            BeanUtils.populate(bean, request.getParameterMap());
            return bean;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

 

数据库:XML,解析Dom4J

标签:tin   put   document   getc   null   stream   col   void   save   

原文地址:https://www.cnblogs.com/xiarongjin/p/8367676.html

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