Java工程师-17周Java数据库开发基础-第二章(XML)

98 阅读1分钟

第二章 XML

image.png

image.png

2.1 XML语法规范

image.png

image.png

image.png

2.2 XML语义约束

2.2.1 DTD

image.png

image.png

image.png

image.png

  • hr:当前XML跟标签
  • SYSTEM:代表同目录
  • "hr.dtd":同路径下dtd文件的文件名

2.2.2 XML Schema

image.png

XML Schema文件的扩展名为xsd

<?xml version="1.0" encoding="UTF-8" ?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.imooc.com/xml-schema" elementFormDefault="qualified">
    <element name="hr">
        <complexType>
            <sequence>
                <element name="employee" minOccurs="0" maxOccurs="99999">
                    <complexType>
                        <sequence>
                            <element name="name" type="string"></element>
                            <element name="age">
                                <simpleType>
                                    <restriction base="integer">
                                        <minInclusive value="18"></minInclusive>
                                        <maxInclusive value="65"></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>
  • xmlns:命名空间,idea自动生成,不需要记忆
  • element:用于说明文档的标签
  • complexType与sequence:用于书写父子标签
  • minOccurs和maxOccurs:最大出现次数和最小出现次数
  • simpleType:限制范围
  • minInclusive:包含的最小值
  • minExclusive:不包含的最小值
  • use:required是必须存在
  • targetNamespace:目标网址,相当于当前文档引用的别名,xml引用时使用
  • elementFormDefault:是否为全局默认文档约束

image.png

引用时,xmlns的值为xsd的targetNamespace;xmlns:xsi是固定的,为了是xsd生效;xsi:schemaLocation的值有两个,一个是targetNamespace,一个是xsd的文件名