package org.domxml;
import org.bean.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.Iterator;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class NoteDom {
Document doc;
public NoteDom() {
doc = PublicDom.getDoc(File.getUrl("Note.xml"));
}
// 查询所有记录
public ArrayList selectAll() {
ArrayList list = new ArrayList();
NoteBean nb = null;
NodeList nList = doc.getElementsByTagName("note");
for (int i = 0; i < nList.getLength(); i++) {
Element element = (Element) nList.item(i);
String id = element.getElementsByTagName("id").item(0)
.getFirstChild().getNodeValue();
String title = element.getElementsByTagName("title").item(0)
.getFirstChild().getNodeValue();
String author = element.getElementsByTagName("author").item(0)
.getFirstChild().getNodeValue();
String matter = element.getElementsByTagName("matter").item(0)
.getFirstChild().getNodeValue();
nb = new NoteBean(Integer.parseInt(id), title, author, matter);
list.add(nb);
}
return list;
}
// 插入记录
public boolean insert(NoteBean nb) {
Element note = doc.createElement("note");
Element id = doc.createElement("id");
Element title = doc.createElement("title");
Element author = doc.createElement("author");
Element matter = doc.createElement("matter");
Element root = doc.getDocumentElement();// 获得跟节点notes
NodeList nlist = root.getElementsByTagName("note");
String idvalue = null;
for (int i = 0; i < nlist.getLength(); i++) {
Element element = (Element) nlist.item(i);
NodeList ids = element.getElementsByTagName("id");
idvalue = ids.item(0).getFirstChild().getNodeValue();
}
Text t_id = doc.createTextNode("" + (Integer.parseInt(idvalue) + 1));
Text t_title = doc.createTextNode(nb.getTitle());
Text t_author = doc.createTextNode(nb.getAuthor());
Text t_matter = doc.createTextNode(nb.getMatter());
id.appendChild(t_id);
title.appendChild(t_title);
author.appendChild(t_author);
matter.appendChild(t_matter);
note.appendChild(id);
note.appendChild(title);
note.appendChild(author);
note.appendChild(matter);
Node node = root.appendChild(note);
PublicDom.getTrans(doc);
return node != null ? true : false;
}
// 更新记录
public boolean update(NoteBean nb) {
Element root = doc.getDocumentElement();
NodeList nodeList = root.getElementsByTagName("note");
NodeList nlist = doc.getElementsByTagName("note");
for (int i = 0; i < nlist.getLength(); i++) {
Element element = (Element) nlist.item(i);
String id = element.getElementsByTagName("id").item(0)
.getFirstChild().getNodeValue();
if (id.equals(new Integer(nb.getId()).toString())) {
element.getElementsByTagName("title").item(0).getFirstChild()
.setNodeValue(nb.getTitle());
element.getElementsByTagName("author").item(0).getFirstChild()
.setNodeValue(nb.getAuthor());
element.getElementsByTagName("matter").item(0).getFirstChild()
.setNodeValue(nb.getMatter());
PublicDom.getTrans(doc);
return true;
}
}
return false;
}
// 删除记录
public boolean delete(NoteBean nb) {
Element root = doc.getDocumentElement();
NodeList nodeList = root.getElementsByTagName("note");
NodeList nlist = doc.getElementsByTagName("note");
for (int i = 0; i < nlist.getLength(); i++) {
Element element = (Element) nlist.item(i);
String id = element.getElementsByTagName("id").item(0)
.getFirstChild().getNodeValue();
if (id.equals(new Integer(nb.getId()).toString())) {
Node node = root.removeChild(nodeList.item(i));
PublicDom.getTrans(doc);
return true;
}
}
return false;
}
// 根据id查找所有记录
public NoteBean select(NoteBean nb) {
NodeList nList = doc.getElementsByTagName("note");
for (int i = 0; i < nList.getLength(); i++) {
Element element = (Element) nList.item(i);
String id = element.getElementsByTagName("id").item(0)
.getFirstChild().getNodeValue();
if (id.equals(new Integer(nb.getId()).toString())) {
String title = element.getElementsByTagName("title").item(0)
.getFirstChild().getNodeValue();
String author = element.getElementsByTagName("author").item(0)
.getFirstChild().getNodeValue();
String matter = element.getElementsByTagName("matter").item(0)
.getFirstChild().getNodeValue();
NoteBean nbs = new NoteBean(Integer.parseInt(id), title,
author, matter);
return nbs;
}
}
return null;
}
// 根据关键字查找所有记录
public ArrayList select(String key) {
ArrayList list = new ArrayList();
NodeList nList = doc.getElementsByTagName("note");
for (int i = 0; i < nList.getLength(); i++) {
Element element = (Element) nList.item(i);
String id = element.getElementsByTagName("id").item(0)
.getFirstChild().getNodeValue();
String title = element.getElementsByTagName("title").item(0)
.getFirstChild().getNodeValue();
String author = element.getElementsByTagName("author").item(0)
.getFirstChild().getNodeValue();
String matter = element.getElementsByTagName("matter").item(0)
.getFirstChild().getNodeValue();
boolean is_title = getKey(title, key);
boolean is_author = getKey(author, key);
boolean is_matter = getKey(matter, key);
if (is_title || is_author || is_matter) {
NoteBean nbean = new NoteBean(title, author, matter);
list.add(nbean);
}
}
return list;
}
public static boolean getKey(String st, String key) {
char[] list = st.toCharArray();
for (int i = 0; i < list.length; i++) {
if (Character.toString(list[i]).equals(key)) {
return true;
}
}
return false;
}
}