Database: XML, parsing dom4j

Source: Internet
Author: User
Tags dateformat

 PackageCom.itheima.util;ImportJava.io.FileOutputStream;ImportJava.net.URL;Importorg.dom4j.Document;ImportOrg.dom4j.io.OutputFormat;ImportOrg.dom4j.io.SAXReader;ImportOrg.dom4j.io.XMLWriter; Public classDom4jutil {Private StaticString Xmlrealpath; Static{ClassLoader cl= Dom4jutil.class. getClassLoader (); URL URL= Cl.getresource ("Users.xml"); Xmlrealpath=Url.getpath (); }         Public StaticDocument GetDocument ()throwsexception{Saxreader Reader=NewSaxreader ();//return Reader.read ("Src/users.xml");//Error        returnReader.read (Xmlrealpath); }     Public Static voidWrite2xml (document Document)throwsexception{XMLWriter writer=NewXMLWriter (NewFileOutputStream (Xmlrealpath), Outputformat.createprettyprint ());        Writer.write (document);    Writer.close (); }}
 PackageCom.itheima.dao.impl;ImportJava.text.DateFormat;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importjava.util.List;Importorg.dom4j.Document;Importorg.dom4j.Element;ImportOrg.dom4j.Node;ImportCom.itheima.dao.UserDao;ImportCom.itheima.domain.User;ImportCom.itheima.util.Dom4JUtil; Public classUserdaoxmlimplImplementsUserdao { PublicUser 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=NewSimpleDateFormat ("Yyyy-mm-dd"); Date Birthday=Df.parse (Xmlbirthday); User User=NewUser (node.valueof ("@username"), Node.valueof ("@password"), Node.valueof ("@email"), birthday); returnuser; } Catch(Exception e) {Throw NewRuntimeException (e); }    }    //<user username= "abc" password= "123" email= "[email protected]" birthday= " 1980-10-01"/>     Public voidSave (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 NewRuntimeException (e); }    }     PublicUser 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=NewSimpleDateFormat ("Yyyy-mm-dd"); Date Birthday=Df.parse (Xmlbirthday); User User=NewUser (node.valueof ("@username"), Node.valueof ("@password"), Node.valueof ("@email"), birthday); returnuser; } Catch(Exception e) {Throw NewRuntimeException (e); }    }}
 PackageCom.itheima.dao.impl;ImportJava.text.DateFormat;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importjava.util.List;Importorg.dom4j.Document;Importorg.dom4j.Element;ImportOrg.dom4j.Node;ImportCom.itheima.dao.UserDao;ImportCom.itheima.domain.User;ImportCom.itheima.util.Dom4JUtil; Public classUserdaoxmlimplImplementsUserdao { PublicUser 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=NewSimpleDateFormat ("Yyyy-mm-dd"); Date Birthday=Df.parse (Xmlbirthday); User User=NewUser (node.valueof ("@username"), Node.valueof ("@password"), Node.valueof ("@email"), birthday); returnuser; } Catch(Exception e) {Throw NewRuntimeException (e); }    }    //<user username= "abc" password= "123" email= "[email protected]" birthday= " 1980-10-01"/>     Public voidSave (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 NewRuntimeException (e); }    }     PublicUser 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=NewSimpleDateFormat ("Yyyy-mm-dd"); Date Birthday=Df.parse (Xmlbirthday); User User=NewUser (node.valueof ("@username"), Node.valueof ("@password"), Node.valueof ("@email"), birthday); returnuser; } Catch(Exception e) {Throw NewRuntimeException (e); }    }}
 Packagecom.itheima.test;Import Staticorg.junit.assert.*;Importjava.util.Date;Importorg.junit.Test;ImportCom.itheima.domain.User;Importcom.itheima.exception.UserHasExistException;ImportCom.itheima.service.BusinessService;ImportCom.itheima.service.impl.BusinessServiceImpl; Public classBusinessserviceimpltest {PrivateBusinessservice s =NewBusinessserviceimpl (); @Test Public voidTestregist ()throwsuserhasexistexception {User User=NewUser ("wzhting", "123", "[email protected]",NewDate ());    S.regist (user); } @Test (Expected=com.itheima.exception.userhasexistexception.class)     Public voidTestRegist1 ()throwsuserhasexistexception {User User=NewUser ("wzhting", "123", "[email protected]",NewDate ());    S.regist (user); } @Test Public voidTestlogin () {User User= S.login ("wzhting", "123");        Assertnotnull (user); User= S.login ("wzhting", "111");        Assertnull (user); User= S.login ("asf", "111");    Assertnull (user); }}
 PackageCom.itheima.util;Importjavax.servlet.http.HttpServletRequest;Importorg.apache.commons.beanutils.BeanUtils; Public classBeanutil {//encapsulates the request parameters into the specified JavaBean     Public Static<T> T Fillbean (httpservletrequest request,class<t>clazz) {        Try{T Bean=clazz.newinstance ();            Beanutils.populate (Bean, Request.getparametermap ()); returnBean; } Catch(Exception e) {Throw NewRuntimeException (e); }    }}

Database: XML, parsing dom4j

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.