首页>代码>java servlet+xml开发联系人增删改查项目>/htmlContacts/src/Dao/ContactOperatorImp.java
package Dao;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class ContactOperatorImp implements ContactOperator {

	public void addContact(Contact contact) {
		// TODO Auto-generated method stub
		try {
			File file = new File("d:/contact.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("phone").setText(contact.getPhone());
			contactElem.addElement("age").setText(contact.getAge());
			contactElem.addElement("email").setText(contact.getEmail());
			contactElem.addElement("qq").setText(contact.getQq());

			FileOutputStream out = new FileOutputStream("d:/contact.xml");
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("utf-8");
			XMLWriter writer = new XMLWriter(out, format);
			writer.write(doc);
			writer.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public void updateContact(Contact contact) {
		// TODO Auto-generated method stub
		try {
			Document doc = new SAXReader().read("d:/contact.xml");
			Element contactEle = (Element) doc
					.selectSingleNode("//contact[@id='" + contact.getId()
							+ "']");
			contactEle.element("name").setText(contact.getName());
			contactEle.element("gender").setText(contact.getGender());
			contactEle.element("phone").setText(contact.getPhone());
			contactEle.element("age").setText(contact.getAge());
			contactEle.element("email").setText(contact.getEmail());
			contactEle.element("qq").setText(contact.getQq());
			FileOutputStream out = new FileOutputStream("d:/contact.xml");
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("utf-8");
			XMLWriter writer = new XMLWriter(out, format);
			writer.write(doc);
			writer.close();
		} catch (Exception e) {
			throw new RuntimeException();
		}
	}

	public void deleteContact(String id) {
		// TODO Auto-generated method stub
		try {
			Document doc = new SAXReader().read("d:/contact.xml");
			Element el = (Element) doc.selectSingleNode("//contact[@id='" + id
					+ "']");
			el.detach();
			FileOutputStream out = new FileOutputStream("d:/contact.xml");
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("utf-8");
			XMLWriter writer = new XMLWriter(out, format);
			writer.write(doc);
			writer.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public List<Contact> findAll() {
		// TODO Auto-generated method stub
		try {
			Document doc = new SAXReader().read("d:/contact.xml");
			List<Contact> listc=new ArrayList<Contact>();
			List<Element> lists = (List<Element>) doc.selectNodes("//contact");
			for(Element list:lists){
				Contact contact=new Contact();
				contact.setId(list.attributeValue("id"));
				contact.setName(list.elementText("name"));
				contact.setAge(list.elementText("age"));
				contact.setEmail(list.elementText("email"));
				contact.setGender(list.elementText("gender"));
				contact.setPhone(list.elementText("phone"));
				contact.setQq(list.elementText("qq"));
				listc.add(contact);
			}
			return listc;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	 public Contact findById(String id) {
		 Document doc = null;
		try {
			doc = new SAXReader().read("d:/contact.xml");
		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	        Element e = (Element)doc.selectSingleNode("//contact[@id='"+id+"']");
	        
	        Contact c = null;
	        if(e!=null){
	            //´´½¨COntact¶ÔÏó
	            c = new Contact();
	            c.setId(e.attributeValue("id"));
	            c.setName(e.elementText("name"));
	            c.setGender(e.elementText("gender"));
	            c.setAge(e.elementText("age"));
	            c.setPhone(e.elementText("phone"));
	            c.setEmail(e.elementText("email"));
	            c.setQq(e.elementText("qq"));
	        }
	        return c;
	    }

}
最近下载更多
晚风吹过你的脸颊  LV1 2023年6月13日
撒野请你吃狗粮  LV2 2021年6月22日
我家大宝贝  LV4 2021年6月10日
苦逼搬砖工  LV3 2021年6月2日
abc5566  LV2 2021年5月29日
408673614  LV2 2021年3月15日
kaola1231  LV4 2020年12月19日
h012345678  LV8 2020年12月15日
ffffffff123  LV4 2020年12月11日
zhaoxiaodao  LV8 2020年11月18日
最近浏览更多
tkggddm  LV3 1月27日
asddwh  LV12 2023年12月25日
微信网友_6783897029726208 2023年12月18日
暂无贡献等级
zdfxcv  LV1 2023年10月14日
2036495585  LV9 2023年9月18日
keumarches 2023年8月31日
暂无贡献等级
652654393  LV2 2023年7月3日
Dhhddhdh 2023年6月24日
暂无贡献等级
晚风吹过你的脸颊  LV1 2023年6月13日
baowanhong  LV2 2023年6月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友