XML
2022-08-11 本文已影响0人
东方奇迹
XML支持五种实体引用:
< < 小于
> > 大于
& & 和号
' ‘ 单引号
" “ 双引号
CDATA标签:指的是不应有XML解析器进行解析的文本数据
格式:<![CDATA[文本数据]]>
XML语义约束方式:DTD文(文档类型定义,扩展名为.dtd)与XML Schema
XML引用DTD文件格式:<!DOCTYPE 根节点 SYSTEM "dtd文件路径">
hehe.dtd
<?xml version="1.0" encoding="UTF-8" ?>
<!ELEMENT hr (employee+)>
<!ELEMENT employee (name,age,salary,department)>
<!ATTLIST employee no CDATA "">
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT salary (#PCDATA)>
<!ELEMENT department (dname,address)>
<!ELEMENT dname (#PCDATA)>
<!ELEMENT address (#PCDATA)>
hehe.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hr SYSTEM "hehe.dtd">
<hr>
<employee no="3309">
<name>张三</name>
<age>31</age>
<salary>4000</salary>
<department>
<dname>会计部</dname>
<address>xx大厦-B103</address>
</department>
</employee>
</hr>
XML Schema比DTD更为复杂,提供了更多功能
hehe.xsd
<?xml version="1.0" encoding="UTF-8" ?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<element name="hr">
<!--complexType标签含义是复杂节点,包含子节点时必须使用这个标签-->
<complexType>
<sequence>
<element name="employee" minOccurs="1" maxOccurs="9999">
<complexType>
<sequence>
<element name="name" type="string"></element>
<element name="age">
<simpleType>
<restriction base="integer">
<minInclusive value="18"></minInclusive>
<maxInclusive value="60"></maxInclusive>
</restriction>
</simpleType>
</element>
<element name="salary" type="integer"></element>
<element name="department">
<complexType>
<sequence>
<element name="dname" type="string"></element>
<element name="address" type="string"></element>
</sequence>
</complexType>
</element>
</sequence>
<attribute name="no" type="string" use="required"></attribute>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
hehe.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 人力资源管理系统 -->
<hr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="hr.xsd">
<employee no="3309">
<name>张三</name>
<age>31</age>
<salary>4000</salary>
<department>
<dname>会计部</dname>
<address>XX大厦-B103</address>
</department>
</employee>
<employee no="3310">
<name>李四</name>
<age>23</age>
<salary>3000</salary>
<department>
<dname>工程部</dname>
<address>XX大厦-B104</address>
</department>
</employee>
</hr>
DOM文档对象模型
Dom4j是一个易用的、开源的库,用于解析XML。它应用于Java平台,具有性能优异、功能强大和极其易使用的特点。
Dom4j将XML视为Document对象。
XML标签被Dom4j定义为Element对象。
读取XML文件
public class TestXml {
public void readXml(){
String file = "/Users/jxb/Desktop/java/xml/src/main/resources/hr.xml";
//SAXReader类是读取XML文件的核心类,用于将XML解析后以"树"的形式保存在内存中;
SAXReader reader = new SAXReader();
try {
Document document = reader.read(file);
//获取XML文档的根节点,即hr标签
Element root = document.getRootElement();
//elements方法用于获取指定的标签集合
List<Element> employees = root.elements("employee");
for (Element employee : employees) {
//element方法用于获取唯一的子节点对象
Element name = employee.element("name");
//getText方法用户获取标签文本
String empName = name.getText();
System.out.println(empName);
System.out.println(employee.elementText("age"));
System.out.println(employee.elementText("salary"));
Element department = employee.element("department");
System.out.println(department.elementText("dname"));
System.out.println(department.elementText("address"));
Attribute attribute = employee.attribute("no");
System.out.println(attribute.getText());
}
} catch (DocumentException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
TestXml testXml = new TestXml();
testXml.readXml();
}
}
写入XML文件
public class HrWriter {
public void writeXml(){
String file = "/Users/jxb/Desktop/java/xml/src/main/resources/hr.xml";
SAXReader reader = new SAXReader();
Document document = null;
try {
document = reader.read(file);
Element root = document.getRootElement();
Element employee = root.addElement("employee");
employee.addAttribute("no", "3311");
Element name = employee.addElement("name");
name.setText("李铁柱");
employee.addElement("age").setText("24");
employee.addElement("salary").setText("3600");
Element department = employee.addElement("department");
department.addElement("dname").setText("人事部");
department.addElement("address").setText("xx大厦-B105");
Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
document.write(writer);
writer.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
HrWriter hrWriter = new HrWriter();
hrWriter.writeXml();
}
}
XPath路径表达式
截屏2022-08-11 10.23.31.png 截屏2022-08-11 10.23.45.png 截屏2022-08-11 10.24.09.png 截屏2022-08-11 10.24.55.png
public class XPathTestor {
public void xpath(String xpathExp) {
String file = "/Users/aisino/Desktop/java/xml/src/main/resources/hr.xml";
SAXReader reader = new SAXReader();
try {
Document document = reader.read(file);
List<Node> nodes = document.selectNodes(xpathExp);
for (Node node : nodes) {
Element emp = (Element) node;
System.out.println(emp.attributeValue("no"));
System.out.println(emp.elementText("name"));
System.out.println(emp.elementText("age"));
System.out.println(emp.elementText("salary"));
System.out.println("=============================");
}
} catch (DocumentException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
XPathTestor testor = new XPathTestor();
// testor.xpath("/hr/employee");
// testor.xpath("//employee");
// testor.xpath("//employee[salary<3600]");
// testor.xpath("//employee[name='李铁柱']");
// testor.xpath("//employee[@no=3311]");
// testor.xpath("//employee[1]");
// testor.xpath("//employee[last()]");
// testor.xpath("//employee[position()<3]");
testor.xpath("//employee[1] | //employee[3]");
}
}