//增删改查文件
package xml2;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.XMLReader;
public class Dom4jReadExmple {
public File file =new File(System.getProperty("user.dir")+File.separator+"dd"+File.separator+"hy.xml");
//遍历所有
public void getSelectedNodeValue(List<Student> list){
if(!file.exists()){
System.out.println("文件不存在");
}
SAXReader saxReader = new SAXReader();
try {
Document document= saxReader.read(file);
Element rootElement= document.getRootElement();
Iterator iterator=document.selectNodes("//student").iterator();
while(iterator.hasNext()){
Student student = new Student();
College college = new College();
Element elem =(Element) iterator.next();
student.setAge(Integer.parseInt(elem.attributeValue("age")==null?"0":elem.attributeValue("age")));
student.setName(elem.element("name").getText());
college.setName(elem.element("college").getText());
college.setLeader(elem.element("college").attributeValue("leader"));
student.setCollege(college);
student.setTelephone(elem.elementText("telephone"));
student.setNote(elem.elementText("notes"));
System.out.println(student.getName());
System.out.println(student.getAge());
System.out.println(student.getTelephone());
System.out.println(student.getCollege().getName());
System.out.println(student.getCollege().getLeader());
System.out.println(student.getNote());
System.out.println("============================");
list.add(student);
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//插入数据
public void insertIntoXML(Student stu){
try {
if(!file.exists()){
file.createNewFile();
PrintWriter writer =new PrintWriter(new FileOutputStream(file));
writer.write("<?xml version='1.0' encoding='UTF-8'?>");
writer.write("
");
writer.write("<all>");
writer.write("
");
writer.write("</all>");
writer.flush();
writer.close();
// 或
// BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
// bufferedWriter.write("<?xml version='1.0' encoding='UTF-8'?>");
// bufferedWriter.write("
");
// bufferedWriter.write("<all>");
// bufferedWriter.write("
");
// bufferedWriter.write("</all>");
// bufferedWriter.flush();
// bufferedWriter.close();
}
Document document= DocumentHelper.createDocument();
Element root=document.addElement("students");
root.addComment("所有部门信息");
Element element =root.addElement("student");
element.addAttribute("id", stu.getId().toString());
element.addElement("age").addText(stu.getAge().toString());
element.addElement("name").addText(stu.getName());
Element element2 =element.addElement("college");
element2.addText(stu.getCollege().getName());
element2.addAttribute("leader", stu.getCollege().getLeader());
element.addElement("telephone").addText(stu.getTelephone());
element.addElement("notes").addText(stu.getNote());
SAXReader saxReader = new SAXReader();
Document document2=saxReader.read(file);
if(null!=document2){
List lists=document2.selectNodes("//student");
if(null!=lists&&lists.size()>0){
Iterator iterator =lists.iterator();
while(iterator.hasNext()){
Element element4 =(Element) iterator.next();
Element element5 =root.addElement("student");
element5.addAttribute("id", element4.attributeValue("id"));
element5.addElement("age").addText(element4.elementText("age"));
element5.addElement("name").addText(element4.elementText("name"));
element5.addElement("college").addText(element4.elementText("college")).addAttribute("leader", element4.element("college").attributeValue("leader"));
element5.addElement("telephone").addText(element4.elementText("telephone"));
element5.addElement("notes").addText(element4.elementText("notes"));
}
}
}
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(file));
xmlWriter.write(document);
xmlWriter.close();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 通过id删除
public void deleteFormXml(int id){
SAXReader saxReader = new SAXReader();
try {
Document document=saxReader.read(file);
Iterator iterator=document.selectNodes("//student[@id]").iterator();
while(iterator.hasNext()){
Element element =(Element) iterator.next();
if(element.attributeValue("id").equals(String.valueOf(id))){
document.getRootElement().remove(element);
}
}
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(file));
xmlWriter.write(document);
xmlWriter.flush();
xmlWriter.close();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 修改对应id数据
public void editXML(Student stu){
SAXReader saxReader =new SAXReader();
try {
Document document =saxReader.read(file);
Iterator iterator=document.selectNodes("//student[@id]").iterator();
while(iterator.hasNext()){
Element element =(Element) iterator.next();
if(element.attributeValue("id").equals(String.valueOf(stu.getId()))){
element.element("name").setText(stu.getName());
element.element("age").setText(stu.getAge().toString());
element.element("college").setText(stu.getCollege().getName());
element.element("college").setAttributeValue("leader", stu.getCollege().getLeader());
element.element("telephone").setText(stu.getTelephone());
element.element("notes").setText(stu.getNote());
}
}
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(file));
xmlWriter.write(document);
xmlWriter.flush();
xmlWriter.close();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}