1.xml文件:
<?xml version="1.0" encoding="UTF-8"?><Students>
<student id="2">
<name>ttt</name>
<age>44</age>
</student>
<student id="3">
<name>linda2</name>
<age>22</age>
</student>
<student id="4">
<name>linda3</name>
<age>23</age>
</student>
<student id="5">
<name>jack</name>
<age>2</age>
</student>
<student id="1">
<name>yyh1</name>
<age>22</age>
</student>
</Students>
2.Java代码
import java.io.File;
import java.io.IOException;
import java.util.Scanner; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException; //在学生管理系统里面,学生的学号是唯一的,姓名有可能重复
public class StudentManager {
public static void main(String[] args) {
try {
Document doc = Domutils.getDoc(new File("xml文件的相对路径"));
Scanner input = new Scanner(System.in);
System.out.println("欢迎来到学生管理系统\n\n\n请输入你要进行什么操作是:\n1.添加学生信息\n2.删除学生信息\n3.修改学生信息\n(请输入前边的序号)");
int num = input.nextInt();
if(num == 1) {
addStudent(doc);
}else if(num == 2) {
delStudent(doc);
}else if(num == 3) {
updStudent(doc);
}
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
//修改学生信息
private static void updStudent(Document doc) {
Element updStudent = null;
Scanner input = new Scanner(System.in);
System.out.println("请输入你要修改的学生的学号:");
String studentid = input.nextLine();
System.out.println("请输入新学生的姓名:");
String newName = input.nextLine();
System.out.println("请输入新学生的年龄:");
String newAge = input.nextLine(); //将每一个学生的列出来,for循环判断你要修改信息的学生是哪一个
NodeList list = doc.getElementsByTagName("student");
for(int i = 0; i <list.getLength(); i++) {
if(studentid.equals(list.item(i).getAttributes().getNamedItem("id").getNodeValue())){
updStudent = (Element) doc.getElementsByTagName("student").item(i).getFirstChild().getParentNode();
//对学生的name属性进行赋新值
updStudent.getElementsByTagName("name").item(i).getFirstChild().setNodeValue(newName);
//对学生的age 属性赋新值
updStudent.getElementsByTagName("age").item(i).getFirstChild().setNodeValue(newAge); }else{
break;
}
}
//找出根元素,将修改后的元素持久化到文件
Element root = doc.getDocumentElement();
transform(root);
System.out.println(updStudent);
}
//删除学生信息
private static void delStudent(Document doc) {
Scanner input = new Scanner(System.in);
//输入你要删除的学生的 学号
System.out.println("请输入要删除学生的学号:");
String studentid = input.nextLine();
Element root = doc.getDocumentElement(); //将学生列成一个表,进行遍历,找对应学号的学生进行删除
NodeList list = doc.getElementsByTagName("student");
for(int i = 0; i < list.getLength(); i++) {
if((studentid).equals(list.item(i).getAttributes().getNamedItem("id").getNodeValue())){
Element delStudent = (Element) doc.getElementsByTagName("student").item(i).getFirstChild().getParentNode();
root.removeChild(delStudent);
break;
}else {
System.out.println("没有该学生");
break;
}
}
//持久化到文件
transform(root);
} //添加学生信息
private static void addStudent(Document doc) {
// System.out.println(doc.getElementsByTagName("student").item(1).getAttributes().getNamedItem("id").getNodeValue());
Element root = doc.getDocumentElement();
//从控制台输入
Scanner input = new Scanner(System.in);
System.out.println("请输入学生的序号:id = "); //将学生放到一个列表里面,看我们要添加的学生的学号里面是否已经有了,如果有,需要将新加入的学生的学号改一下
NodeList list = doc.getElementsByTagName("student");
String studentid = input.nextLine();
for(int i = 0; i < list.getLength(); i++) {
if(studentid.equals(list.item(i).getAttributes().getNamedItem("id").getNodeValue())){
System.out.println("该序号学生表里面已经存在,请重新输入一个新的序号:");
studentid = input.nextLine();
}else {
break;
}
} System.out.println("请输入要添加学生的姓名:name = ");
String name_value = input.nextLine();
System.out.println("请输入要添加学生的年龄:age = ");
String age_value = input.nextLine(); //创建节点
Element student = doc.createElement("student");
Element name = doc.createElement("name");
Element age = doc.createElement("age");
Text namText = doc.createTextNode(name_value);
Text ageText = doc.createTextNode(age_value);
//关联节点之间的关系
root.appendChild(student);
student.appendChild(name);
student.appendChild(age);
student.setAttribute("id", studentid);
name.appendChild(namText);
age.appendChild(ageText);
//持久化到文件
transform(root); }
//持久化到文件的方法
private static void transform(Element root)
throws TransformerFactoryConfigurationError {
TransformerFactory factory = TransformerFactory.newInstance();
try {
Transformer tf = factory.newTransformer();
tf.transform(new DOMSource(root), new StreamResult(new File("src/com/briup/dom/student.xml")));
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
2.Dom解析文件(将获取解析文件的部分封装起来)
import java.io.File;
import java.io.IOException;
import java.nio.file.attribute.AclEntry.Builder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document;
import org.xml.sax.SAXException; public class Domutils {
public static Document getDoc(File file) throws SAXException, IOException, ParserConfigurationException {
//获取工厂模式
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
//获取builder对象
DocumentBuilder builder = factory.newDocumentBuilder();
//将要解析文件加载成一个树状文件,开始解析
Document document = builder.parse(file);
return document;
}
}