一、XML的作用
二、XML的语法
● 文档声明必须为结束;
● 文档声明必写在第一行;
1.语法格式:
<?xml version="1.0" encoding="UTF-8"?>
2.属性说明:
● versioin:指定XML文档版本。必须属性,因为我们不会选择1.1,只会选择1.0;
● encoding:指定当前文档的编码。可选属性,默认值是utf-8;
元素的命名规则
- 不能使用空格,不能使用冒号
- xml 标签名称区分大小写
- XML 必须有且只有一个根元素
三、注释
<!-- 注释 -->
四、XML约束
①DTD约束
<!ELEMENT students (student+) >
<!ELEMENT student (name,age,sex)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT sex (#PCDATA)>
<!ATTLIST student number ID #REQUIRED>
<!--
ELEMENT: 用来定义元素
students (student+) : 代表根元素 必须是 <students>
student+ : 根标签中至少有一个 student子元素, + 代表至少一个
student (name,age,sex): student 标签中包含的子元素,按顺序出现
#PCDATA: 是普通文本内容
ATTLIST: 用来定义属性
student number ID #REQUIRED
student子元素中 有一个ID属性叫做 number,是必须填写的
ID: 唯一 值只能是字母或者下划线开头
-->
引入DTD
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE students SYSTEM "student.dtd">
<students>
<student number="s1">
<name>小斌</name>
<age>22</age>
<sex>男</sex></student>
<student number="s2">
<name>广坤</name>
<age>55</age>
<sex>男</sex>
</student>
</students>
②Schema约束
<?xml version="1.0"?>
<xsd:schema xmlns="http://www.lagou.com/xml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.lagou.com/xml"
elementFormDefault="qualified">
<xsd:element name="students" type="studentsType"/>
<xsd:complexType name="studentsType">
<xsd:sequence>
<xsd:element name="student" type="studentType" minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="studentType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="age" type="ageType" />
<xsd:element name="sex" type="sexType" />
</xsd:sequence>
<xsd:attribute name="number" type="numberType" use="required"/>
</xsd:complexType>
<xsd:simpleType name="sexType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="male"/>
<xsd:enumeration value="female"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ageType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="200"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="numberType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="hehe_\d{4}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
XML引入Schema约束
<?xml version="1.0" encoding="UTF-8" ?>
<students
xmlns="http://www.lagou.com/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.lagou.com/xml student.xsd"
>
<student number="hehe_1234">
<name>张百万</name>
<age>25</age>
<sex>female</sex>
</student>
<student number="hehe_0000">
<name>小斌</name>
<age>20</age>
<sex>male</sex>
</student>
</students>
五、XML 解析
● DOM:要求解析器把整个XML文档装载到内存,并解析成一个Document对象。
优点:元素与元素之间保留结构关系,故可以进行增删改查操作。
缺点:XML文档过大,可能出现内存溢出显现。
● SAX:是一种速度更快,更有效的方法。它逐行扫描文档,一边扫描一边解析。并以事件驱动的方式进行具体解析,每执行一行,都将触发对应的事件。
优点:占用内存少 处理速度快,可以处理大文件
缺点:只能读,逐行后将释放资源。
①dom4j 的使用
1.准备xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns="http://www.lagou.com/xml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.lagou.com/xml"
elementFormDefault="qualified">
<xsd:element name="users" type="usersType"/>
<xsd:complexType name="usersType">
<xsd:sequence>
<xsd:element name="user" type="userType" minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="userType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="age" type="ageType" />
<xsd:element name="hobby" type="hobbyType" />
</xsd:sequence>
<xsd:attribute name="id" type="numberType" use="required"/>
</xsd:complexType>
<xsd:simpleType name="ageType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="100"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="hobbyType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="抽烟"/>
<xsd:enumeration value="喝酒"/>
<xsd:enumeration value="烫头"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="numberType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
2.编写user.xml 引入约束
<?xml version="1.0" encoding="UTF-8" ?>
<users
xmlns="http://www.lagou.com/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.lagou.com/xml user.xsd"
>
<user id="1">
<name>张百万</name>
<age>20</age>
<hobby>抽烟</hobby>
</user>
<user id="2">
<name>于谦</name>
<age>50</age>
<hobby>喝酒</hobby>
</user>
<user id="3">
<name>刘能</name>
<age>40</age>
<hobby>烫头</hobby>
</user>
</users>
3.读取xml
public class TestDOM4j {
//获取XML文件中的 所有的元素名称(标签)
@Test
public void test1() throws DocumentException {
//1.获取XML解析对象
SAXReader reader = new SAXReader();
//2.解析XML 获取 文档对象 document
Document document =
reader.read("H:\\jdbc_work\\xml_task03\\src\\com\\lagou\\xml03\\user.xml");
//3.获取根元素
Element rootElement = document.getRootElement();
//获取根元素名称
System.out.println(rootElement.getName());
//获取 根元素下的标签
List<Element> elements = rootElement.elements();
for (Element element : elements) {
System.out.println("根标签下的子节点: " + element.getName());
List<Element> eList = element.elements();
for (Element e : eList) {
System.out.println("user标签下的子节点" + e.getName());
}
break;
}
}
@Test
public void test2() throws DocumentException {
//1.创建XML文档解析对象
SAXReader sr = new SAXReader();
//2.读取XML获取到document对象
Document document = sr.read("src\\com\\lagou\\xml02\\user.xml");
//3.获取根节点
Element rootElement = document.getRootElement();
//4.得到当前节点的 所有子节点
List<Element> elements = rootElement.elements();
//5.获取第一个子节点
Element user = elements.get(0);
//6.获取所有信息
String id = user.attributeValue("id");
String name = user.elementText("name");
String age = user.elementText("age");
//使用getText获取当前元素的文本内容
String hobby = user.element("hobby").getText();
//打印
System.out.println(id+" " + name +" " + age +" " + hobby);
}
}
②xpath方式读取xml
使用dom4j支持xpath的操作的几种主要形式
数据准备 book.xml
<?xml version="1.0" encoding="UTF-8" ?>
<bookstore>
<book id="book1">
<name>金瓶梅</name>
<author>金圣叹</author>
<price>99</price>
</book>
<book id="book2">
<name>红楼梦</name>
<author>曹雪芹</author>
<price>69</price>
</book>
<book id="book3">
<name>Java编程思想</name>
<author>埃克尔</author>
<price>59</price>
</book>
</bookstore>
1.使用selectSingleNode方法 查询指定节点中的内容
/*
* 1. 使用selectSingleNode方法 查询指定节点中的内容
* */
@Test
public void test1() throws DocumentException {
//1.创建解析器对象
SAXReader sr = new SAXReader();
//2.获取文档对象
Document document =
sr.read("H:\\jdbc_work\\xml_task03\\src\\com\\lagou\\xml03\\book.xml");
//3.调用 selectSingleNode() 方法,获取name节点对象
Node node1 = document.selectSingleNode("/bookstore/book/name");
System.out.println("节点: " + node1.getName());
System.out.println("书名: " + node1.getText());
//4.获取第二本书的名称
Node node2 = document.selectSingleNode("/bookstore/book[2]/name");
System.out.println("第二本书的书名为: " + node2.getText());
}
2.使用selectSingleNode方法 获取属性值,或者属性值对应的节点
@Test
public void test2() throws DocumentException {
//1.创建解析器对象
SAXReader sr = new SAXReader();
//2.获取文档对象
Document document =
sr.read("H:\\jdbc_work\\xml_task03\\src\\com\\lagou\\xml03\\book.xml");
//3.获取第一个book节点的 id属性的值
Node node1 = document.selectSingleNode("/bookstore/book/attribute::id");
System.out.println("第一个book的id值为: " + node1.getText());
//4.获取最后一个book节点的 id属性的值
Node node2 =
document.selectSingleNode("/bookstore/book[last()]/attribute::id");
System.out.println("最后一个book节点的id值为: " + node2.getText());
//5.获取id属性值为 book2的 书名
Node node3 = document.selectSingleNode("/bookstore/book[@id='book2']");
String name = node3.selectSingleNode("name").getText();
System.out.println("id为book2的书名是: " + name);
}
3.使用 selectNodes()方法 获取对应名称的所有节点
@Test
public void test3() throws DocumentException {
//1.创建解析器对象
SAXReader sr = new SAXReader();
//2.获取文档对象
Document document =
sr.read("H:\\jdbc_work\\xml_task03\\src\\com\\lagou\\xml03\\book.xml");
//3.获取所有节点,打印节点名
List<Node> list = document.selectNodes("//*");
for (Node node : list) {
System.out.println("节点名: " + node.getName());
}
//4.获取所有的书名
List<Node> names = document.selectNodes("//name");
for (Node name : names) {
System.out.println(name.getText());
}
//5.获取指定 id值为book1的节点的所有 内容
List<Node> book1 =
document.selectNodes("/bookstore/book[@id='book1']//*");
for (Node node : book1) {
System.out.println(node.getName()+" = " + node.getText());
}
}
六、JDBC自定义XML
1.创建自定义xml 文件, 保存 数据库连接信息
<?xml version="1.0" encoding="UTF-8" ?>
<jdbc>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db5?
characterEncoding=UTF-8</property>
<property name="user">root</property>
<property name="password">123456</property>
</jdbc>
2.编写工具类 ,使用xpath 读取数据库信息
public class JDBCUtils {
//1. 定义字符串变量, 记录获取连接所需要的信息
public static String DRIVERNAME;
public static String URL;
public static String USER;
public static String PASSWORD;
//2.静态代码块
static {
try {
//使用 xpath读取 xml中的配置信息
SAXReader sr = new SAXReader();
Document document =
sr.read("H:\\workspace01\\JDBC_day02\\src\\com\\lagou\\xml03\\jdbc-config.xml");
Node node =
document.selectSingleNode("/jdbc/property[@name='driverClass']");
//System.out.println(node.getText());
DRIVERNAME = node.getText();
URL =
document.selectSingleNode("/jdbc/property[@name='jdbcUrl']").getText();
USER =
document.selectSingleNode("/jdbc/property[@name='user']").getText();
PASSWORD =
document.selectSingleNode("/jdbc/property[@name='password']").getText();
//注册驱动
Class.forName(DRIVERNAME);
} catch (Exception e) {
e.printStackTrace();
}
}
//3.获取连接的静态方法
public static Connection getConnection(){
try {
//获取连接对象
Connection connection = DriverManager.getConnection(URL, USER,
PASSWORD);
//返回连接对象
return connection;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}
3.获取所有员工的姓名
//获取所有员工的姓名
public static void main(String[] args) {
try {
//1.获取连接
Connection connection = JDBCUtils.getConnection();
//2.获取 statement ,执行SQL
Statement statement = connection.createStatement();
String sql = "select * from employee";
//3.处理结果集
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
String ename = resultSet.getString("ename");
System.out.println(ename);
}
} catch (SQLException e) {
e.printStackTrace();
}
}