package cn.com.dao;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import cn.com.domain.StudentInfo;
import cn.com.exception.StudentNotExitsException;
import cn.com.util.OperaUtils;
/**
 * 总结:
 * dao层的如果是个异常就往外抛...就会带来更多的麻烦..
 * 但是如果你catch了,只选择了打印...上级在调用时即使发生异常...也会往下执行...
 * 所以可以选择转型,上级或者处理或者不处理..但不处理肯定会影响执行... 
 * Summary1:
 * 既把异常报告给了上层,又没有给上级带来麻烦...(转型为运行时异常..)
 * Summary2:
 * 可以把需要抛出的异常全部放到RuntimeException,然后在UI层进行解析... 但是这样不方便接口测试..
 * 接口测试应该是一个独立的方法测试...并能得到独立的结果...
 * 假如全部封装为RuntimeException无法区分是什么异常...需要在测试方法中另写代码解析,比较繁琐..我说的包装代码详见cn.com.exception.Test.java
 * @author GuoBaoqiang
 * 2014年7月24日
 */
public class StudentDao {
	/**
	 * @descri 学生信息添加操作..
	 */
	public void add(StudentInfo student) {
		Document document;
		try {
			document = OperaUtils.getXMLDocumet();

			Element element = document.createElement("学生");

			Element name = document.createElement("姓名");
			name.setTextContent(student.getName());
			name.setAttribute("idNumber", student.getIdNumber());

			Element address = document.createElement("住址");
			address.setAttribute("address", student.getAddress());

			Element grade = document.createElement("分数");
			grade.setTextContent(student.getGrade() + "");

			element.appendChild(name);
			element.appendChild(address);
			element.appendChild(grade);

			document.getElementsByTagName("学生信息").item(0).appendChild(element);
			OperaUtils.writeToXML(document);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * @descri 查询学生信息
	 */
	public void delete(String name) throws StudentNotExitsException {
		try {
			Document document = OperaUtils.getXMLDocumet();

			NodeList node_list = document.getElementsByTagName("姓名");
			for (int i = 0; node_list.getLength() > 0&& i < node_list.getLength(); i++) {
				if (node_list.item(i).getTextContent().equals(name)) {
					node_list.item(i).getParentNode().getParentNode().removeChild(node_list.item(i).getParentNode());
					OperaUtils.writeToXML(document);
					return;
				}
			}
			throw new StudentNotExitsException();
		} catch (IOException e) {
			throw new RuntimeException(e);
		} catch (TransformerException e) {
			throw new RuntimeException(e);
		} catch (SAXException e) {
			throw new RuntimeException(e);
		} catch (ParserConfigurationException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 
	 * @author Guobaoqiang
	 * @throws IOException
	 * @throws SAXException
	 * @throws ParserConfigurationException
	 * @descri 查询student信息
	 */
	public StudentInfo find(String name) throws StudentNotExitsException {
		// 第二种方法,,,仅供参考...不只有documen.getElementsByTag()...只要是element都可以调用..
		// NodeList node_list2 = document.getElementsByTagName("学生");
		// Element student_tag = (Element)node_list2.item(i);
		// String address =
		// student_tag.getElementsByTagName("住址").item(i).getTextContent();
		// s.setAddress(address);
		try {
			StudentInfo s = new StudentInfo();
			Document document;
			document = OperaUtils.getXMLDocumet();
			NodeList node_list = document.getElementsByTagName("姓名");
			
			for (int i = 0; i < node_list.getLength(); i++) {
				
				if (node_list.item(i).getTextContent().equals(name)) {
					s.setName(name);

					String idNumber = ((Element) node_list.item(i)).getAttribute("idNumber");
					s.setIdNumber(idNumber);

					NodeList node2_list = node_list.item(i).getParentNode().getChildNodes();
					// 第一种方法
					String address = document.getElementsByTagName("住址").item(i).getTextContent();
					s.setAddress(address);
					
					String grade = document.getElementsByTagName("分数").item(i).getTextContent();
					s.setGrade(Double.valueOf(grade));

					return s;
				}
			}
			throw new StudentNotExitsException();
		} catch (IOException e) {
			throw new RuntimeException(e);
		} catch (SAXException e) {
			throw new RuntimeException(e);
		} catch (ParserConfigurationException e) {
			throw new RuntimeException(e);
		}
	}
}
最近下载更多
329512801  LV28 2021年7月8日
18695997580  LV2 2020年11月13日
simple丶余心  LV21 2020年9月16日
Yifan Gao  LV2 2020年2月22日
MMENGDI  LV14 2019年12月17日
3969138  LV15 2019年9月23日
hufuman  LV23 2019年8月19日
maozi123  LV1 2019年6月17日
啊咧D啊咧D  LV1 2019年6月4日
yj980704  LV1 2019年5月25日
最近浏览更多
asddwh  LV12 2023年12月25日
1443251642  LV1 2022年12月19日
yyds123456  LV2 2022年12月2日
wei12356  LV1 2022年1月14日
开发哈哈 2022年1月11日
暂无贡献等级
李晓珂  LV2 2021年12月14日
Dimoo1 2021年11月29日
暂无贡献等级
tangjj7260  LV18 2021年11月12日
Trickster  LV9 2021年10月19日
329512801  LV28 2021年7月8日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友