1.xml格式举例
<?xml version="1.0" encoding="UTF-8"?>
<!-- 人力资源管理系统 -->
<hr>
<employee no="9527">
<name>张三</name>
<age>30</age>
<salary>4000</salary>
<department>
<dname>会计部</dname>
<address>xx大厦-B103</address>
</department>
</employee>
<employee no="9528">
<name>李四</name>
<age>23</age>
<salary>3000</salary>
<department>
<dname>工程部</dname>
<address>xx大厦-B104</address>
</department>
</employee>
</hr>
第一行需要标明xml解析的版本和编码
2.对于xml中特殊字符会影响对字符的解析,有两种方式解决
1.实体引用
2.CDATA标签
3.语义约束
dtd举例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hr SYSTEM "hr.dtd"> #使用dtd文件
<!-- teaching plan-->
<hr>
<employee no="9527">
<name>张三</name>
<age>30</age>
<salary>4000</salary>
<department>
<dname>会计部</dname>
<address>xx大厦-B103</address>
</department>
</employee>
<employee no="9528">
<name>李四</name>
<age>23</age>
<salary>3000</salary>
<department>
<dname>工程部</dname>
<address>xx大厦-B104</address>
</department>
</employee>
</hr>
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT hr (employee+)> #hr下至少一个employee
<!ELEMENT employee (name,age,salary,department)> #注意空格
#描述属性
<!ATTLIST employee no CDATA ""> #CDATA表示节点类型 ""表示没有填写时的默认值
<!ELEMENT name (#PCDATA)> #描述name为纯文本节点
<!ELEMENT age (#PCDATA)> #描述name为纯文本节点
<!ELEMENT salary (#PCDATA)> #描述name为纯文本节点
<!ELEMENT department (dname,adress)>
<!ELEMENT dname (#PCDATA)> #描述name为纯文本节点
schema语义约束
<?xml version="1.0" encoding="UTF-8"?>
<!-- teaching plan-->
<hr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="hr.xsd">
<employee no="9527">
<name>张三</name>
<age>30</age>
<salary>4000</salary>
<department>
<dname>会计部</dname>
<address>xx大厦-B103</address>
</department>
</employee>
<employee no="9528">
<name>李四</name>
<age>23</age>
<salary>3000</salary>
<department>
<dname>工程部</dname>
<address>xx大厦-B104</address>
</department>
</employee>
</hr>
//可以运行,无错误
<?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>
//有错误,但有注释
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<!--complexType标签含义是复杂结点,包含子节点时使用这个标签 -->
<complexType name="hr">
<sequence>
<!-- minOccurs 约束标签个数 最少出现?次 -->
<element name="employee" minOccurs="1" maxOccurs="9999">
<complexType>
<sequence>
<!-- 规定类型只能输入string,不能有子节点 -->
<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>
</complexType>
</element>
</sequence>
<!-- 描述属性 -->
<attribute name="no" type="string" use="required"></attribute>
</complexType>
</schema>