主要内容:
XML:
XML与HTML的比较:
例如:
XML的用途:
XML文档结构:
XML声明:只能放在第一行
XML标签:
合法的标签名:
适当的注释与缩进:
合理使用属性:
处理特殊字符:
实体引用:用替代的方法使用特殊字符
CDATA标签:
有序的子元素:
案例测试:
将其写入xml文档
测试代码:
<?xml version="1.0" encoding="UTF-8"?>
<teaching-plan attribute="教学计划">
<course attribute="课程">
<course-name attribute="课程名">大学英语</course-name>
<class-hour attribute="课时">36</class-hour>
<exam-form attribute="考核方式">考试</exam-form>
</course>
<course attribute="课程">
<course-name attribute="课程名">高等数学</course-name>
<class-hour attribute="课时">70</class-hour>
<exam-form attribute="考核方式">考试</exam-form>
</course>
<course attribute="课程">
<course-name attribute="课程名">计算机应用基础</course-name>
<class-hour attribute="课时">108</class-hour>
<exam-form attribute="考核方式">上机考试</exam-form>
</course>
</teaching-plan>
输出结果:直接将xml文件拖到浏览器中
XML语意约束:
DTD约束:hr (employee) 中间必须添加空格
DTD定义节点:
DTD定义节点数量:
XML引用DTD文件:
创建DTD文件:
测试代码:
<?xml version="1.0" encoding="UTF-8"?>
<!-- XML的规范 -->
<!-- 必须用空格隔开 -->
<!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)>
在XML文件进行导入:SYSTEM代表的是本地文件
<!DOCTYPE hr SYSTEM "hr.dtd">
编程练习:
测试代码:
<?xml version="1.0" encoding="UTF-8"?>
<!-- XML的规范 -->
<!-- 必须用空格隔开 -->
<!ELEMENT teaching-plan (course+)>
<!ELEMENT course (course-name,class-hour,exam-form)>
<!-- 对属性进行定义 -->
<!ATTLIST teaching-plan attribute CDATA "">
<!ATTLIST course attribute CDATA "">
<!ATTLIST course-name attribute CDATA "">
<!ATTLIST class-hour attribute CDATA "">
<!ATTLIST exam-form attribute CDATA "">
<!-- 纯文本节点 -->
<!ELEMENT teaching-plan (#PCDATA)>
<!ELEMENT course (#PCDATA)>
<!ELEMENT course-name (#PCDATA)>
<!ELEMENT class-hour (#PCDATA)>
<!ELEMENT exam-form (#PCDATA)>
XML Schema:
测试代码:
对于下面的添加约束:
<?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>
XML Schema: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>
案例测试:暂时搁置
DOM文档对象模型:
DOM4j:
DOM4j遍历XML:首先将 dom4j-2.2.1.jar包 add path到工程中
测试代码:
package com.imooc.dom4j;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class HrReader {
//读取XML文件方法
public void readXml(){
String file = "/Users/lcs/eclipse-workspace/xml/src/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.print(empName);
//获取属性
Attribute att = employee.attribute("no");
System.out.println(" 工号:"+att.getText());
System.out.println(employee.elementText("age"));
System.out.println(employee.elementText("salary"));
Element department = employee.element("department");
System.out.println(department.element("dname").getText());
System.out.println(department.element("address").getText());
System.out.println("================");
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
HrReader reader = new HrReader();
reader.readXml();
}
}
输出结果:
张三 工号:3309
30
4000
会计部
XX-大厦-B103
================
莉丝 工号:3310
30
5000
工厂部
XX-大厦-B104
================
DOM4j更新XML:写入一个人员信息到hr.xml中
package com.imooc.dom4j;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class HrWriter {
public void writeXml() {
String file = "/Users/lcs/eclipse-workspace/xml/src/hr.xml";
// SAXReader类是读取XML文件的核心类,用于将XML解析后以“树”的形式保存在内存中
SAXReader reader = new SAXReader();
try {
Document document = reader.read(file);
// 获取XML文档的根节点,即hr标签
Element root = document.getRootElement();
// addElement()方法用于创造一个传入参数类型的元素
Element employee = root.addElement("employee");
// 然后在新建的employee上增加属性
employee.addAttribute("no", "3311");
// 增加子节点
Element name = employee.addElement("name");
name.setText("王五");
employee.addElement("age").setText("37");
employee.addElement("salary").setText("9000");
Element department = employee.addElement("department");
department.addElement("dname").setText("人事部");
department.addElement("address").setText("XX-大厦-A108");
// 已经在内存中完成了编写,接下来将其导入到XML文件
Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
document.write(writer);
writer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
HrWriter hrWriter = new HrWriter();
hrWriter.writeXml();
}
}
查看hr.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hr SYSTEM "hr.dtd"><!-- 人力资源管理系统 -->
<hr>
<employee no="3309">
<name>张三</name>
<age>30</age>
<salary>4000</salary>
<department>
<dname>会计部</dname>
<address>XX-大厦-B103</address>
</department>
</employee>
<employee no="3310">
<name>莉丝</name>
<age>30</age>
<salary>5000</salary>
<department>
<dname>工厂部</dname>
<address>XX-大厦-B104</address>
</department>
</employee>
<employee no="3311">
<name>王五</name>
<age>37</age>
<salary>9000</salary>
<department>
<dname>人事部</dname>
<address>XX-大厦-A108</address>
</department>
</employee>
</hr>
案例测试:
写入代码:
package com.imooc.dom4j;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Plan2Writer {
public void writeXml() {
String file = "/Users/lcs/eclipse-workspace/xml/src/plan2.xml";
// SAXReader类是读取XML文件的核心类,用于将XML解析后以“树”的形式保存在内存中
SAXReader reader = new SAXReader();
try {
Document document = reader.read(file);
// 获取XML文档的根节点,即course标签
Element root = document.getRootElement();
// addElement()方法用于创造一个传入参数类型的元素
Element employee = root.addElement("course");
// 增加子节点
Element name = employee.addElement("course-name");
name.setText("历史");
employee.addElement("class-hour").setText("50");
employee.addElement("exam-form").setText("考试");
// 已经在内存中完成了编写,接下来将其导入到XML文件
Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
document.write(writer);
writer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Plan2Writer plan2Writer = new Plan2Writer();
plan2Writer.writeXml();
}
}
打开plan.xml:格式化后结果为
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plan SYSTEM "plan.dtd">
<teaching-plan>
<course>
<course-name>大学英语</course-name>
<class-hour>36</class-hour>
<exam-form>考试</exam-form>
</course>
<course>
<course-name>高等数学</course-name>
<class-hour>70</class-hour>
<exam-form>考试</exam-form>
</course>
<course>
<course-name>计算机应用基础</course-name>
<class-hour>108</class-hour>
<exam-form>上机考试</exam-form>
</course>
<course>
<course-name>历史</course-name>
<class-hour>50</class-hour>
<exam-form>考试</exam-form>
</course>
</teaching-plan>
XPath路径表达式:
XPath基本表达式:
测试案例:
XPath实验室:
测试案例:
package com.imooc.dom4j;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class XPathTestor {
public void xpath(String xpathExp) {
String file = "/Users/lcs/eclipse-workspace/xml/src/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.elementText("name"));
System.out.println("工号:"+emp.attributeValue("no"));
System.out.println("年龄:"+emp.elementText("age"));
System.out.println("薪水:"+emp.elementText("salary"));
System.out.println("==========");
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
XPathTestor testor = new XPathTestor();
//输出根节点下的employee
// testor.xpath("/hr/employee");
//输出所有employee
// testor.xpath("//employee");
//输出salary小于4000的employee
// testor.xpath("//employee[salary<4000]");
//输出名字为李一的employee,用单引号引起来
// testor.xpath("//employee[name='李一']");
//输出属性no为3304的employee,不用单引号引起来
// testor.xpath("//employee[@no=3304]");
//输出第一个employee
// testor.xpath("//employee[1]");
//输出最后employee
// testor.xpath("//employee[last()]");
//按照指定位置查询
// testor.xpath("//employee[position()<6]");
//按照指定位置3和8查询,组合符号 |
testor.xpath("//employee[3] | //employee[8]");
}
}
编程练习:
测试代码:
package com.imooc.dom4j;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class Plan2Testor {
public void xpath(String xpathExp) {
String file = "/Users/lcs/eclipse-workspace/xml/src/plan2.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.elementText("course-name"));
System.out.println("课时:"+emp.elementText("class-hour"));
System.out.println("考核方式:"+emp.elementText("exam-form"));
System.out.println("==========");
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Plan2Testor testor = new Plan2Testor();
//输出所有课程信息
testor.xpath("//course");
//输出课时小于50的course
testor.xpath("//course[class-hour<50]");
//输出名字为高等数学的course,用单引号引起来
testor.xpath("//course[course-name='高等数学']");
//输出前两条位置查询
testor.xpath("//course[position()<3]");
}
}
输出结果:
课程:大学英语
课时:36
考核方式:考试
==========
课程:高等数学
课时:70
考核方式:考试
==========
课程:计算机应用基础
课时:108
考核方式:上机考试
==========
课程:历史
课时:50
考核方式:考试
==========
课程:大学英语
课时:36
考核方式:考试
==========
课程:高等数学
课时:70
考核方式:考试
==========
课程:大学英语
课时:36
考核方式:考试
==========
课程:高等数学
课时:70
考核方式:考试
==========
总结:
get