一、schema
1.语法
<?xml version="1.0" encoding="UTF-8" ?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.cn/123"
elementFormDefault="qualified">
<!-- 这里写标签 -->
</schema>
注
:
创建的schema文件,根节点为<schema>
xmlns="http://www.w3.org/2001/XMLSchema" ——表示当前xml文件是一个约束文件,使用schema
targetNamespace="http://www.example.cn/123" ——该属性值用来xml文件引入约束,不是固定值
- 标签类型
标签分为复合标签和简易标签,格式如下:
<element name="root">
<complexType>
<sequence>
<element name="name" type="string" maxOccurs="unbounded"/>
<element name="age" type="int"/>
</sequence>
</complexType>
</element>
其中root
是复合标签,name
和age
是简易标签,type
为简易标签文本类型,maxOccurs
决定了name
标签可以出现的最大次数,sequence
表示xml文件中name
和age
要严格按顺序排列且必须存在,类似的还有all
,choice
,all
表示name
,age
必须存在,但不要求顺序,choice
表示name
,age
只能存在其一。
- 属性
- 复合元素:
<attribute name="id" type="int" use="required"/>
<!--
写在</complexType>前
name:属性名称
type:属性类型
use:属性是否必须出现
-->
- 简易元素:
<element name="book" minOccurs="1">
<complexType>
<simpleContent>
<extension base="string">
<attribute name="b" type="string"/>
</extension>
</simpleContent>
</complexType>
</element>
二、在xml文件中引入约束文件
1.语法
<?xml version="1.0" encoding="UTF-8"?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.example.cn/123"
xsi:schemaLocation="http://www.example.cn/123 demo.xsd">
</person>
注
:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ——表示xml是一个被约束文件
xmlns="http://www.example.cn/123" ——值为schema文件的targetNamespace
xsi ——不允许有重复的xmlns,所以起别名
xsi:schemaLocation ——引用的约束文件位置
一个xml文件可以引用多个schema约束文件,如果标签名重合,要用别名区分
三、实例
- schema文件:
<?xml version="1.0" encoding="UTF-8" ?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.cn/123"
elementFormDefault="qualified">
<element name="person">
<complexType>
<sequence>
<!--<all>-->
<!--<choice>-->
<element name="name" type="string" maxOccurs="unbounded"/>
<element name="age" type="int"/>
<any minOccurs="0"/>
<!--</choice>-->
<!--</all>-->
</sequence>
<attribute name="id" type="int" use="required"/>
</complexType>
</element>
</schema>
- xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.example.cn/123"
xsi:schemaLocation="http://www.example.cn/123 demo.xsd" id="3">
<name>zhangsan</name>
<age>19</age>
</person>
四、sax解析
- sax不能实现增删改操作
- 需要继承事件处理器,重写其中三个方法
- 示例如下:
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
public class TestSax {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse("./src/com/example/schema/person/person.xml", new MyHandler());
}
}
class MyHandler extends DefaultHandler {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
System.out.print("<" + qName + ">");
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
System.out.print("</" + qName + ">");
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
System.out.print(new String(ch, start, length));
}
}