如何 增删改查 XML文件中的元素
students.xml 文件如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><students>
<student id="2">
<name>张三</name>
<address>泸职院</address>
<email>zhangsan@163.com</email>
</student>
<student id="1">
<name>李四</name>
<address>泸职院22</address>
<email>lisi@163.com</email>
</student>
<student id="4">
<name>王五</name>
<address>aaaa</address>
<email>wangwu@163.com</email>
</student>
<student id="3">
<name>张美丽</name>
<address>西南医学院</address>
<email>wangmeili@163.com</email>
</student>
</students>
具体实现如下:
创建文件位置关系如下:
Student 类文件如下:
package demo7;
public class Student {
private String id;
private String name;
private String address;
private String email;
public Student() {
}
public Student(String id, String name, String address, String email) {
this.id = id;
this.name = name;
this.address = address;
this.email = email;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
", email='" + email + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Demo1 类 文件如下:
package demo7;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
//XML
public class Demo1 {
public static void main(String[] args) {
Demo1 demo1 = new Demo1();
Student student = demo1.selectOneById("1");
System.out.println(student);
// demo1.insert(new Student("6","晨颖","西安","166@163.com"));
// demo1.update(new Student("6","晨颖","西安","adc@163.com"));
// demo1.delete("6");
ArrayList<Student> students = demo1.selectAll();
for (Student student : students) {
System.out.println(student);
}
}
//通过ID 查找 XML中元素的对象
public Student selectOneById(String stuId) {
try {
//获取工厂对象
DocumentBuilderFactory doc = DocumentBuilderFactory.newInstance();
//获取构建对象
DocumentBuilder dc = doc.newDocumentBuilder();
//解析路获得文档对象
Document document = dc.parse(new File("src\\students.xml"));
//获得到了 文件的根元素 名称 students
Element element = document.getDocumentElement();
//获取所有student元素
NodeList nodeList = element.getElementsByTagName("student");
//getLength() 获取集合元素的数量 (student 元素的数量)
for (int i = 0; i < nodeList.getLength(); i++) {
//遍历 nodeList 集合 通过下标 获取 每个元素student
Element student = (Element) nodeList.item(i);
//获取student元素的id属性值
String id = student.getAttribute("id");
if (stuId.equals(id)) {
//通过getTextContent()方法 获取 元素的值 (标签之间的值)
//item(0) 其中0代表 name标签 的个数
String name = student.getElementsByTagName("name").item(0).getTextContent();
String address = student.getElementsByTagName("address").item(0).getTextContent();
String email = student.getElementsByTagName("email").item(0).getTextContent();
//组装对象
Student stu = new Student(id, name, address, email);
return stu;
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
//查询所有数据
public ArrayList<Student> selectAll() {
ArrayList<Student> studentArrayList = new ArrayList<>();
try {
DocumentBuilderFactory doc = DocumentBuilderFactory.newInstance();
DocumentBuilder dc = doc.newDocumentBuilder();
Document document = dc.parse(new File("src\\students.xml"));
Element element = document.getDocumentElement();
NodeList nodeList = element.getElementsByTagName("student");
for (int i = 0; i < nodeList.getLength(); i++) {
Element student = (Element) nodeList.item(i);
String id = student.getAttribute("id");
String name = student.getElementsByTagName("name").item(0).getTextContent();
String address = student.getElementsByTagName("address").item(0).getTextContent();
String email = student.getElementsByTagName("email").item(0).getTextContent();
Student stu = new Student(id, name, address, email);
studentArrayList.add(stu);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return studentArrayList;
}
//增加一个student元素
public void insert(Student student) {
try {
DocumentBuilderFactory doc = DocumentBuilderFactory.newInstance();
DocumentBuilder dc = doc.newDocumentBuilder();
Document document = dc.parse(new File("src\\students.xml"));
//根元素
Element element = document.getDocumentElement();
//通过 creatElement(元素名称) 创建新的元素
Element newstudent = document.createElement("student");
// 通过 setAttribute(“属性名”,“属性值”) 为新的节点 增加属性
newstudent.setAttribute("id", student.getId());
// 创建 name address email
Element name = document.createElement("name");
Element address = document.createElement("address");
Element email = document.createElement("email");
// 为元素 赋值
name.setTextContent(student.getName());
address.setTextContent(student.getAddress());
email.setTextContent(student.getEmail());
// 组装 层次关系
newstudent.appendChild(name);
newstudent.appendChild(address);
newstudent.appendChild(email);
// 将student元素 添加到 students 根节点下
element.appendChild(newstudent);
// 同步 将内存中的xml树 写入到文件中
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult("src\\students.xml"));
//new DOMSource(解析后文件) new StreamResult(目标文件)
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
//改变元素中的值
public void update(Student student) {
try {
DocumentBuilderFactory doc = DocumentBuilderFactory.newInstance();
DocumentBuilder dc = doc.newDocumentBuilder();
Document document = dc.parse(new File("src\\students.xml"));
//根元素
Element element = document.getDocumentElement();
//获取所有student元素
NodeList nodeList = element.getElementsByTagName("student");
for (int i = 0; i < nodeList.getLength(); i++) {
Element students = (Element) nodeList.item(i);
String id = students.getAttribute("id");
// 通过 id 判断是否是需要修改的对象
if (id.equals(student.getId())) {
students.getElementsByTagName("name").item(0).setTextContent(student.getName());
students.getElementsByTagName("address").item(0).setTextContent(student.getAddress());
students.getElementsByTagName("email").item(0).setTextContent(student.getEmail());
}
}
// 同步 将内存中的xml树 写入到文件中
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult("src\\students.xml"));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
//通过Id删除XML中的元素对象
public void delete(String stuId) {
try {
DocumentBuilderFactory doc = DocumentBuilderFactory.newInstance();
DocumentBuilder dc = doc.newDocumentBuilder();
Document document = dc.parse(new File("src\\students.xml"));
Element element = document.getDocumentElement();
NodeList nodeList = element.getElementsByTagName("student");
for (int i = 0; i < nodeList.getLength(); i++) {
Element student = (Element) nodeList.item(i);
String id = student.getAttribute("id");
if (stuId.equals(id)) {
// 删除子节点
element.removeChild(student);
}
}
// 同步 将内存中的xml树 写入到文件中
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult("src\\students.xml"));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}