contact.java
/** * contact实体 * @author Administrator * */ public class contact { private String id; private String name; private String gender; private String email; private int age; private String phone; private String qq; public void setId(String id) { this.id = id; } public void setName(String name) { this.name = name; } public void setGender(String gender) { this.gender = gender; } public void setEmail(String email) { this.email = email; } public void setAge(int age) { this.age = age; } public void setPhone(String phone) { this.phone = phone; } public void setQq(String qq) { this.qq = qq; } public String getId() { return id; } public String getName() { return name; } public String getGender() { return gender; } public String getEmail() { return email; } public int getAge() { return age; } public String getPhone() { return phone; } public String getQq() { return qq; } @Override public String toString() { return "contact [id=" + id + ", name=" + name + ", gender=" + gender + ", email=" + email + ", age=" + age + ", phone=" + phone + ", qq=" + qq + "]"; } }
contactoperator.java
/** * 联系人相关操作的方法 * @author Administrator * */ public interface contactoperator { public void addcontact(contact contact); public void updatecontact(contact contact); public void deletecontact(String id); public List<contact> findall(); }
contactoperatorimpl.java
public class contactoperatorimpl implements contactoperator { // public void addcontact(contact contact); // public void updatecontact(); // public void deletecontact(); // public List<contact> findall(); public void addcontact(contact contact){ // TODO Auto-generated method stub try { File file= new File("e:/contact1.xml"); Document doc=null; Element rootelem = null; if(!file.exists()){ doc=DocumentHelper.createDocument(); rootelem=doc.addElement("contactlist"); }else{ doc= new SAXReader().read(file); rootelem=doc.getRootElement(); } Element contactElem = rootelem.addElement("contact"); contactElem.addAttribute("id", contact.getId()); contactElem.addElement("name").setText(contact.getName()); contactElem.addElement("gender").setText(contact.getGender()); contactElem.addElement("age").setText(contact.getAge()+""); contactElem.addElement("phone").setText(contact.getPhone()); contactElem.addElement("email").setText(contact.getEmail()); contactElem.addElement("qq").setText(contact.getQq()); xmlunit.write2xml(doc); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void updatecontact(contact contact) { // TODO Auto-generated method stub Document doc=xmlunit.getDocument(); Element contactelem=(Element) doc.selectSingleNode("//contact[@id='"+contact.getId()+"']"); contactelem.element("name").setText(contact.getName()); contactelem.element("gender").setText(contact.getGender()); contactelem.element("age").setText(contact.getAge()); contactelem.element("phone").setText(contact.getPhone()); contactelem.element("email").setText(contact.getEmail()); contactelem.element("qq").setText(contact.getQq()); xmlunit.write2xml(doc); } public void deletecontact(String id ) { // TODO Auto-generated method stub Document doc=xmlunit.getDocument(); Element contactelem=(Element) doc.selectSingleNode("//contact[@id='"+id+"']"); contactelem.detach(); xmlunit.write2xml(doc); } public List<contact> findall() { // TODO Auto-generated method stub Document doc = xmlunit.getDocument(); //2.创建List对象 List<contact > list = new ArrayList<contact>(); //3.读取contact标签 List<Element> conList = (List<Element>)doc.selectNodes("//contact"); for(Element e:conList){ //创建COntact对象 contact c = new contact(); c.setId(e.attributeValue("id")); c.setName(e.elementText("name")); c.setGender(e.elementText("gender")); c.setAge(Integer.parseInt(e.elementText("age"))); c.setPhone(e.elementText("phone")); c.setEmail(e.elementText("email")); c.setQq(e.elementText("qq")); //把Contact放入list中 list.add(c); } return list; } }
xmlunit.java 公共工具类 抽取常用方法
public class xmlunit { public static Document getDocument(){ try { Document doc = new SAXReader().read(new File("e:/contact1.xml")); return doc; } catch (DocumentException e) { e.printStackTrace(); throw new RuntimeException(e); } } public static void write2xml(Document doc){ try { FileOutputStream out= new FileOutputStream("e:/contact1.xml"); OutputFormat format=OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter writer= new XMLWriter(out,format); writer.write(doc); writer.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
测试代码:
/** * 联系人操作实现类的测试类 * @author APPle * */ public class TestContactOperatorImpl { ContactOperator operator = null; /** * 初始化对象实例 */ @Before public void init(){ operator = new ContactOperatorImpl(); } @Test public void testAddContact(){ Contact contact = new Contact(); contact.setId("3"); contact.setName("rose"); contact.setGender("男"); contact.setAge(20); contact.setPhone("134222233333"); contact.setEmail("eric@qq.com"); contact.setQq("33334444"); operator.addContact(contact); } @Test public void testUpdateContact(){ Contact contact = new Contact(); contact.setId("3"); //修改的ID contact.setName("zhangsan"); contact.setGender("女"); contact.setAge(30); contact.setPhone("135222233333"); contact.setEmail("zhangsan@qq.com"); contact.setQq("33334444"); operator.updateContact(contact); } @Test public void testDeleteContact(){ operator.deleteContact("3"); } @Test public void testFindAll(){ List<Contact> list = operator.findAll(); for (Contact contact : list) { System.out.println(contact); } } }