用戶: userjava
id,name,pwddom
聯繫人:contactui
id,name,telspa
第一步:設計xml和格式設計
第二步:設置項目的結構code
第三步:開發兩個javaBean用於數據的封裝orm
第四步:爲了避免用每次都去讀取xml文件,全部,應該將dom對象單一的實例xml
package cn.utils; import java.io.File; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class DomFactory { private static Document dom; private static File file; static { try { file =new File( ClassLoader.getSystemResource("contact.xml").getPath()); dom = DocumentBuilderFactory .newInstance() .newDocumentBuilder() .parse(file); } catch (Exception e) { throw new RuntimeException(e);//轉換異常 } } //用工廠模式返回這個domn public static Document getDocument(){ return dom; } //保存的功能 public static void transform(){ try { Transformer ts = TransformerFactory.newInstance().newTransformer(); ts.transform(new DOMSource(dom), new StreamResult(file)); } catch (Exception e) { e.printStackTrace(); } } }
第五步:開發dao對象
import cn.domain.User; import cn.utils.DomFactory; public class ContactDao { public boolean login(User user){ boolean boo =false; //遍歷dom Document dom = DomFactory.getDocument(); NodeList nl = dom.getElementsByTagName("user"); for(int i=0,len=nl.getLength();i<len;i++){ Element e = (Element) nl.item(i); String name =e.getElementsByTagName("name").item(0).getTextContent(); String pwd = e.getElementsByTagName("pwd").item(0).getTextContent(); if(name.equals(user.getName())&& pwd.equals(user.getPwd())){ boo = true; break; } } return boo; } }
第六步:開發action開發
package cn.contact; import java.util.List; import java.util.Scanner; import cn.contact.dao.ContactDao; import cn.domain.Contact; import cn.domain.User; public class ContactAction { public static void main(String[] args) { new ContactAction(); } Scanner sc = new Scanner(System.in); private ContactDao dao; ContactAction(){ dao = new ContactDao(); while(true){ System.err.println("1:login\n2:reg\n3:exit"); String op = sc.nextLine(); if(op.equals("1")){ login(); }else if(op.equals("2")){ reg(); }else if(op.equals("3")){ break; } } } private void login(){ System.err.println("plz enter name:"); String name = sc.nextLine(); String pwd = sc.nextLine(); User u = new User(); u.setName(name); u.setPwd(pwd); boolean boo =dao.login(u); if(boo){ System.err.println("login success.."); //查詢全部聯繫人 operation(); }else{ System.err.println("name or password is eerror"); } } private void reg(){ String name = sc.nextLine(); String pwd = sc.nextLine(); User u = new User(); u.setName(name); u.setPwd(pwd); dao.reg(u); } public void operation(){ while(true){ System.err.println("1:show all\n2:del\n3:update\n4:exit\n5:add"); String op = sc.nextLine(); if(op.equals("1")){ showAll(); }else if(op.equals("5")){ add(); }else if(op.equals("4")){ break; } } } private void add() { String name = sc.nextLine(); String tel = sc.nextLine(); Contact c = new Contact(); c.setName(name); c.setTel(tel); dao.add(c); } public void showAll(){ List<Contact> list = dao.showALl(); System.err.println("ID\t\t\t\tName\t\ttel"); for(Contact c :list){ System.err.println(c.getId()+"\t\t\t\t"+c.getName()+"\t\t\t"+c.getTel()); } } }