XML文件
XML :Extensible Markup Language 可扩展标记语言
XML文档在项目主要用来存配制信息,熟悉XML文档数据编写
解析XML :在JDK中有API,可以解析,但不方便(读),在实际应用中,一般用第三方开源组件 (dom4j)
DOM:对HTML文档,基DOM称为HTML DOM ,对XML文档,其DOM称为XML DOM
DOM :Document Object Model 文档对象模型,在我们加载文档到内存中后,会自动构建一个DOM模型(是一个倒立节点树)
在DOM节点树中,主要有三种类型的节点:元素节点、属性节点、文本节点
解析XML文件
Java Bean
package cn.lanqiao.xml;
/**
* @author 阿木木
*/ //实体类
public class Student {
private String sid;
private String sname;
private String smajor;
private String stel;
public Student(String sid,String sname,String smajor,String stel) {
this.sid = sid;
this.sname = sname;
this.smajor = smajor;
this.stel = stel;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSmajor() {
return smajor;
}
public void setSmajor(String smajor) {
this.smajor = smajor;
}
public String getStel() {
return stel;
}
public void setStel(String stel) {
this.stel = stel;
}
@Override
public String toString() {
return "Student{" +
"sid='" + sid + '\'' +
", sname='" + sname + '\'' +
", smajor='" + smajor + '\'' +
", stel='" + stel + '\'' +
'}';
}
}
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student sid="2">
<sname>张三</sname>
<smajor>软件工程</smajor>
<stel>130.......</stel>
</student>
<student sid="3">
<sname>李四</sname>
<smajor>软件工程</smajor>
<stel>130...</stel>
</student>
</students>
<!--
约定大于配制 : 约束
-->
操作类
package cn.lanqiao.xml;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author 阿木木
*/
public class Dom4JDemo {
//读取xml文件;
static final String URL="xmlproject/src/students.xml";
public static void main(String[] args) throws Exception {
//添加数据
//Student student=new Student("3","廖灵妹","软件工程","130...");
//add(student);
//获取所有数据
//List<Student> students = listAll();
//System.out.println(students);
//修改数据;
//stu.setSmajor("dddd");
//stu.setSname("dddd");
//stu.setStel("dddd");
//update(stu);
// Student stu =get("1");
//删除数据;
// delete("1");
}
public static void delete(String sid){
try{
SAXReader reader = new SAXReader(); //创建一个流对象;
//获取根元素;
Document document = null; //返回一个文档对象 --- 内存中的DOM
document = reader.read(URL);
Element root = document.getRootElement();
List<Element> list = root.elements("student");
Element findElement = null;
for(Element e :list){
if(e.attributeValue("sid").equals(sid)){
findElement = e;
break;
}
}
if(findElement!=null){
root.remove(findElement);
}
//保存
OutputFormat format = OutputFormat.createPrettyPrint(); //格式化;
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(URL),format);
xmlWriter.write(document);
xmlWriter.close();
}catch (Exception e){e.printStackTrace();}
}
public static Student get(String sid){
Student stu =null;
try{
SAXReader reader = new SAXReader(); //创建一个流对象;
//获取根元素;
Document document = null; //返回一个文档对象 --- 内存中的DOM
document = reader.read(URL);
Element root = document.getRootElement();
//找到要修改的元素;
List<Element> list = root.elements("student");
Element findElement = null;
for(Element e :list){
if(e.attributeValue("sid").equals(sid)){
findElement = e;
break;
}
}
if(findElement!=null){
stu=new Student(findElement.attributeValue("sid"),findElement.elementText("sname"),findElement.elementText("smajor"),findElement.elementText("stel"));
}
}catch (Exception e){e.printStackTrace();}
return stu;
}
public static void update(Student student){
try{
SAXReader reader = new SAXReader(); //创建一个流对象;
//获取根元素;
Document document = null; //返回一个文档对象 --- 内存中的DOM
document = reader.read(URL);
Element root = document.getRootElement();
//找到要修改的元素;
List<Element> list = root.elements("student");
Element upateElement = null;
for(Element e :list){
if(e.attributeValue("sid").equals(student.getSid())){
upateElement = e;
break;
}
}
//实现修改
if(upateElement!=null){
upateElement.element("sname").setText(student.getSname());
upateElement.element("smajor").setText(student.getSmajor());
upateElement.element("stel").setText(student.getStel());
}
//保存
OutputFormat format = OutputFormat.createPrettyPrint(); //格式化;
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(URL),format);
xmlWriter.write(document);
xmlWriter.close();
}catch (Exception e){e.printStackTrace();}
}
public static void add(Student student){
try {
SAXReader reader = new SAXReader(); //创建一个流对象;
//获取根元素;
Document document = null; //返回一个文档对象 --- 内存中的DOM
document = reader.read(URL);
Element root = document.getRootElement();
//在根元素的下面添加一个子元素(student);
Element addStudent = root.addElement("student");
//设置元素属性;
addStudent.addAttribute("sid",student.getSid());
//添加子元素sname,smajor,stel;
addStudent.addElement("sname").setText(student.getSname());
addStudent.addElement("smajor").setText(student.getSmajor());
addStudent.addElement("stel").setText(student.getStel());
//持久化 --将document对象写入磁盘文件;
OutputFormat format = OutputFormat.createPrettyPrint(); //格式化;
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(URL),format);
xmlWriter.write(document);
xmlWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static List<Student> listAll(){
List<Student> list=new ArrayList<>();
//加载xml文件 --- 流
SAXReader reader = new SAXReader(); //创建一个流对象;
try {
Document document = reader.read(URL); //返回一个文档对象
//获取根元素;
Element root = document.getRootElement();
//获取根元素下的子元素(student);
Iterator<Element> it= root.elementIterator("student");
Student stu = null;
while(it.hasNext()){
Element student = it.next();
//取属性;
String sid =student.attributeValue("sid");
//获取子元素(sname,smajor,stel);
String sname = student.elementText("sname");
String smajor = student.elementText("smajor");
String stel = student.elementText("stel");
stu = new Student(sid,sname,smajor,stel);
list.add(stu);
}
} catch (DocumentException e) {
e.printStackTrace();
}
return list;
}
}
读取
添加
修改
删除
**约束:**能文档语法、格式等加载规则 ----- 需要你遵守 ,XML文档的约束可以使用2种方式:DTD | Schema ,可以为某一个XML文档编写DTD或者Schema,但一般没有必要