XML Schema
发布时间:2020-12-16 23:32:47 所属栏目:百科 来源:网络整理
导读:xml文件: 1 ?xml version="1.0" encoding="utf-8"? 2 orders !-- 所有订单信息 -- 3 order !-- 订单,至少包含1个订单 -- 4 orderDate2018-05-20/orderDate!--orderDate为日期型 -- 5 shipTo country="CN"!-- 配送信息,country属性必须出现 -- 6 name张三
xml文件: 1 <?xml version="1.0" encoding="utf-8"?> 2 <orders> <!-- 所有订单信息 --> 3 <order> <!-- 订单,至少包含1个订单 --> 4 <orderDate>2018-05-20</orderDate><!--orderDate为日期型 --> 5 <shipTo country="CN"><!-- 配送信息,country属性必须出现 --> 6 <name>张三峰</name> <!-- 收件人,长度小于50 --> 7 <street>市中区滨河路778号5+3大酒店</street> <!-- 县/区及街道地址 --> 8 <city>乐山市</city> <!-- 市/区,长度小于50 --> 9 <state>四川省</state> <!-- 省/自治区/直辖市,长度小于50 --> 10 <phone>13999999999</phone> <!-- 联系电话,要求必须是1开头,后面第2位数字3-9,再后面是9个数字 --> 11 </shipTo> 12 <items> <!-- 商品列表,item应至少出现1次 --> 13 <item partNum="872-AA"> <!-- 商品编号属性,必须有 --> 14 <productName>香辣鸡翅</productName><!-- 商品名称,长度小于50 --> 15 <quantity>1</quantity> <!-- 购买数量,至少是1 --> 16 <price>18.95</price> <!-- 单价为浮点数,大于0.0 --> 17 <shipDate>2018-05-21</shipDate> <!-- 配送日期 --> 18 </item> 19 <item partNum="926-AA"> 20 <productName>烧烤五花肉</productName> 21 <quantity>20</quantity> 22 <price>39.98</price> 23 <shipDate>2018-05-20</shipDate> 24 </item> 25 </items> 26 </order> 27 </orders> xsd文件: 1 <?xml version="1.0" encoding="UTF-8"?> 2 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" > 3 <xsd:element name="orders"> 4 <xsd:complexType> 5 <xsd:sequence> 6 <xsd:element ref="order" minOccurs="1"></xsd:element> 7 </xsd:sequence> 8 </xsd:complexType> 9 </xsd:element> 10 11 <xsd:element name="order" > 12 <xsd:complexType> 13 <xsd:sequence> 14 <xsd:element ref="orderDate"></xsd:element> 15 <xsd:element ref="shipTo"></xsd:element> 16 <xsd:element ref="items" minOccurs="1"></xsd:element> 17 </xsd:sequence> 18 </xsd:complexType> 19 </xsd:element> 20 21 <xsd:element name="orderDate" type="xsd:date"></xsd:element> 22 <xsd:element name="shipTo"> 23 <xsd:complexType> 24 <xsd:sequence> 25 <xsd:element ref="name"></xsd:element> 26 <xsd:element ref="street"></xsd:element> 27 <xsd:element ref="city"></xsd:element> 28 <xsd:element ref="state"></xsd:element> 29 <xsd:element ref="phone"></xsd:element> 30 </xsd:sequence> 31 <xsd:attribute name="country" type="xsd:string" use="required"></xsd:attribute> 32 </xsd:complexType> 33 </xsd:element> 34 <xsd:element name="items"> 35 <xsd:complexType> 36 <xsd:sequence> 37 <xsd:element ref="item" minOccurs="1" maxOccurs="unbounded"></xsd:element> 38 </xsd:sequence> 39 </xsd:complexType> 40 </xsd:element> 41 42 <xsd:element name="item"> 43 <xsd:complexType> 44 <xsd:sequence> 45 <xsd:element ref="productName"></xsd:element> 46 <xsd:element ref="quantity"></xsd:element> 47 <xsd:element ref="price"></xsd:element> 48 <xsd:element ref="shipDate"></xsd:element> 49 </xsd:sequence> 50 <xsd:attribute name="partNum" type="xsd:string" use="required"></xsd:attribute> 51 </xsd:complexType> 52 </xsd:element> 53 54 <xsd:element name="name" type="nameType"></xsd:element> 55 <xsd:element name="street" type="xsd:string"></xsd:element> 56 <xsd:element name="city" type="nameType"></xsd:element> 57 <xsd:element name="state" type="nameType"></xsd:element> 58 <xsd:element name="phone" type="phoneType"></xsd:element> 59 60 <xsd:element name="productName" type="nameType"></xsd:element> 61 <xsd:element name="quantity" type="xsd:positiveInteger"></xsd:element> 62 <xsd:element name="price" type="priceType"></xsd:element> 63 <xsd:element name="shipDate" type="xsd:date"></xsd:element> 64 65 66 <xsd:simpleType name="nameType"> 67 <xsd:restriction base="xsd:string"> 68 <xsd:minLength value="0"></xsd:minLength> 69 <xsd:maxLength value="50"></xsd:maxLength> 70 </xsd:restriction> 71 </xsd:simpleType> 72 <xsd:simpleType name="phoneType"> 73 <xsd:restriction base="xsd:integer"> 74 <xsd:pattern value="1[3-9]{1}[0-9]{9}"></xsd:pattern> 75 </xsd:restriction> 76 </xsd:simpleType> 77 <xsd:simpleType name="priceType"> 78 <xsd:restriction base="xsd:float"> 79 <xsd:minInclusive value="0.1"></xsd:minInclusive> 80 </xsd:restriction> 81 </xsd:simpleType> 82 83 84 </xsd:schema> 验证XML的Java文件: 1 import java.io.File; 2 3 import javax.xml.transform.stream.StreamSource; 4 import javax.xml.validation.Schema; 5 import javax.xml.validation.SchemaFactory; 6 import javax.xml.validation.Validator; 7 8 import org.xml.sax.SAXException; 9 import org.xml.sax.SAXParseException; 10 import org.xml.sax.helpers.DefaultHandler; 11 12 public class TestSchema { 13 14 public static void main(String[] args) { 15 File xsdfile = new File("order.xsd"); 16 File xmlfile = new File("orders.xml"); 17 Handler errorHandler = null; 18 try { 19 SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); 20 Schema schema = schemaFactory.newSchema(xsdfile); 21 Validator validator = schema.newValidator(); 22 errorHandler = new Handler(); 23 validator.setErrorHandler(errorHandler); 24 validator.validate(new StreamSource(xmlfile)); 25 } catch (Exception e) { 26 System.out.println(e); 27 } 28 if (errorHandler.errorMessage == null) { 29 System.out.println("XML 文件:" + xmlfile.getName()+"符合模式"); 30 } else { 31 System.out.println("XML 文件:" + xmlfile.getName()+"不符合模式"); 32 } 33 } 34 } 35 36 class Handler extends DefaultHandler { 37 String errorMessage = null; 38 39 public void error(SAXParseException e) throws SAXException { 40 errorMessage = e.getMessage(); 41 int row = e.getLineNumber(); 42 int colums = e.getColumnNumber(); 43 System.out.println("一般错误"+errorMessage+"位置:"+row+","+colums); 44 } 45 46 public void fatalError(SAXParseException e) throws SAXException { 47 errorMessage = e.getMessage(); 48 int row = e.getLineNumber(); 49 int colums = e.getColumnNumber(); 50 System.out.println("致命错误"+errorMessage+"位置:"+row+","+colums); 51 } 52 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |