XML(EXtensible Markup Language):可扩展标记语言
XML 和 HTML 之间的差异
王者荣耀
000000
1000
吃鸡
000000
1000
用以表明该文件是一个XML文档
对于任何一个XML文档,其声明部分都是固定的格式
用<>括起来的各种标签来标记数据
<标签名>标签描述的内容标签名>
000000
任何一个XML文件都可以包含任意数量的标签
元素由开始标签、元素内容和标签组成。元素内可以包含子元素、字符数据等
<元素名 属性名 = "属性值">元素内容元素名>
王者荣耀
000000
1000
每个XML文档有且只有一个根元素
<元素名 属性名 = "属性值" 属性名 = "属性值">元素内容元素名>
王者荣耀
000000
1000
在XML文档中,有时在元素的文本中会出现一些特殊字符(如<、>、'、"、&),为了正确的解析这些特殊字符内容,可以使用转义符和CDATA节
DOM4J的常用接口都在org.dom4j包里定义
接口名 | 说明 |
Attribute | 定义了XML的属性 |
Document | 定义了XML文档 |
Element | 定义了XML元素 |
Node | 为DOM4J中所有的XML节点定义了多态行为 |
package day07.XML;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import java.io.File;
/**
* @author: zkz
* @datetime: 2023/12/27
* @desc:
*/
public class Dom4jUtil {
public static Document createDocument(String xmlPath) {
//1.指定要解析的XML文件
File file = new File(xmlPath);
//2.创建文档对象模型
SAXReader sax = new SAXReader();
Document document = null;
try {
document = sax.read(file);
} catch (DocumentException e) {
System.out.println("文档不存在!");
}
return document;
}
}
package day07.XML;
/**
* @author: zkz
* @datetime: 2023/12/27
* @desc:
*/
public class Member {
private String card;
private String name;
private String password;
private int count;
public Member() {
}
public Member(String card, String name, String password, int count) {
this.card = card;
this.name = name;
this.password = password;
this.count = count;
}
@Override
public String toString() {
return "Member{" +
"card='" + card + '\'' +
", name='" + name + '\'' +
", password='" + password + '\'' +
", count=" + count +
'}';
}
public String getCard() {
return card;
}
public void setCard(String card) {
this.card = card;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
package day07.XML;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author: zkz
* @datetime: 2023/12/27
* @desc:
*/
public class MemberDom4J {
//先创建一个空的Member集合,用来存储解析出来的每一个Member对象
List memberList = new ArrayList<>();
//解析XML文件
public void loadXml(String xmlPath) {
Document document = Dom4jUtil.createDocument(xmlPath);
if(document == null){
System.out.println("文档解析失败!");
return;
}
Member member = null;
//1.获取到根节点 members
Element rootElement = document.getRootElement();
//2.获取到根节点的所有子节点 每一个member元素
List memberEleList = rootElement.elements();
for (Element memberEle : memberEleList) {
//3.创建每一个member元素对应的Member对象
member = new Member();
//4.解析member元素的card属性值,并赋值给Member对象的card属性
member.setCard(memberEle.attributeValue("card"));
//直接从member元素中获取指定名称的子元素,如下面这个就是获取指定的name标签的文本内容,并赋值给Member的name属性
member.setName(memberEle.element("name").getText());
member.setPassword(memberEle.element("password").getText());
int count = 0;
try {
count = Integer.parseInt(memberEle.element("count").getText());
}catch (Exception e){}
member.setCount(count);
//8.每解析玩一个member元素,就表示创建完了一个Member对象,将这个对象添加到事先准备好的集合中
memberList.add(member);
}
}
//添加一个Member对象
public void addMember(Member member){
Document document = Dom4jUtil.createDocument("D:\\KD50\\project\\API\\src\\day07\\XML\\MemberInformation.xml");
Element rootElement = document.getRootElement();
Element memberEle = rootElement.addElement("member");
memberEle.addAttribute("card",member.getCard());
memberEle.addElement("name").setText(member.getName());
memberEle.addElement("password").setText(member.getPassword());
memberEle.addElement("count").setText(member.getCount()+"");
saveXml(document,"D:\\KD50\\project\\API\\src\\day07\\XML\\saveMember.xml");
}
//修改
public void updateMember(Member member){
Document document = Dom4jUtil.createDocument("D:\\KD50\\project\\API\\src\\day07\\XML\\MemberInformation.xml");
Element rootElement = document.getRootElement();
List memberEleList = rootElement.elements();
for (Element memberEle : memberEleList) {
if(memberEle.attributeValue("card").equals(member.getCard())){
memberEle.element("name").setText(member.getName());
memberEle.element("password").setText(member.getPassword());
memberEle.element("count").setText(member.getCount()+"");
break;
}
}
saveXml(document,"D:\\KD50\\project\\API\\src\\day07\\XML\\updateMember.xml");
}
//修改
public void deleteMember(String card){
Document document = Dom4jUtil.createDocument("D:\\KD50\\project\\API\\src\\day07\\XML\\MemberInformation.xml");
Element rootElement = document.getRootElement();
List memberEleList = rootElement.elements();
for (Element memberEle : memberEleList) {
if(memberEle.attributeValue("card").equals(card)){
//rootElement.remove(memberEle);
memberEle.getParent().remove(memberEle);
break;
}
}
saveXml(document,"D:\\KD50\\project\\API\\src\\day07\\XML\\deleteMember.xml");
}
public void saveXml(Document document ,String savePath){
//1.创建XMLWriter对象
XMLWriter xmlWriter = null;
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
try {
xmlWriter = new XMLWriter(new FileWriter(savePath),format);
xmlWriter.write(document);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(xmlWriter!=null){
xmlWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void print() {
memberList.forEach(member -> {
System.out.println(member);
});
}
}
package day07.XML;
/**
* @author: zkz
* @datetime: 2023/12/27
* @desc:
*/
public class Test {
public static void main(String[] args) {
MemberDom4J dom4J = new MemberDom4J();
/*dom4J.loadXml("files/xml/members.xml");
dom4J.print();*/
/*Member member = new Member("VIP1003","QQ飞车","123456",1500);
dom4J.addMember(member);*/
/*Member member = new Member("VIP1001","绝地求生","000000",2500);
dom4J.updateMember(member);*/
dom4J.deleteMember("VIP1000");
}
}