使用XSD验证XML
本文简单介绍如何使用XSD验证自己的xml。项目中我们经常使用xml作为配置或者数据交互的文件格式,在解析XML前最好验证一下或者在生成xml文件后验证一下生成的文件格式是正确,这样可以避免解析时出现一些低级的xml文件不符合预定义格式的错误。
CSDN无法正确显示这些xsd, xml以及java代码,如果需要请参考http://yqbjtu.blog.163.com/blog/static/529426201672674444341/
第一步显示定义XSD,这个参考网上很多教程,本文给出一个简单实例。
order_v1.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Licensed Text
-->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://demo.com/OrderXMLSchema_v1"
xmlns:tns="http://demo.com/OrderXMLSchema_v1">
<xsd:element name="shipOrder" type="tns:shipOrder"></xsd:element>
<xsd:complexType name="shipOrder">
<xsd:sequence maxOccurs="1" minOccurs="1">
<xsd:element name="orderperson" type="xsd:string" minOccurs="1" maxOccurs="1"></xsd:element>
<xsd:element name="order-description" type="xsd:string" minOccurs="1" maxOccurs="1"></xsd:element>
<xsd:element name="items" type="tns:items" maxOccurs="1" minOccurs="1"></xsd:element>
<xsd:element name="item-2" type="tns:item-2" maxOccurs="unbounded" minOccurs="1"></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="items">
<xsd:sequence maxOccurs="unbounded" minOccurs="1">
<xsd:element name="item" minOccurs="1" maxOccurs="1">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="index" type="xsd:unsignedLong" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="item-2">
<xsd:sequence maxOccurs="1" minOccurs="1">
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="note" type="xsd:string" minOccurs="0"/>
<xsd:element name="quantity" type="xsd:positiveInteger"/>
<xsd:element name="price" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="index" type="xsd:unsignedLong" />
</xsd:complexType>
</xsd:schema>
Xml文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
- Licensed Description.
-->
<shipOrder xmlns="http://demo.com/OrderXMLSchema_v1"
xmlns:server="http://demo.com/OrderXMLSchema_v1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<orderperson>John Smith</orderperson>
<order-description>This document is used for demo sxN#</order-description>
<items>
<item index="1">
some potatoes
</item>
<item index="2">
some tomatoes
</item>
</items>
<item-2 index="1">
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item-2>
<item-2 index="2">
<title>Hide your heart</title>
<note>this is for testing note, it is not required</note>
<quantity>1</quantity>
<price>9.90</price>
</item-2>
</shipOrder>
最后是我们的java代码
import java.io.File;
import java.io.UnsupportedEncodingException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;
public class index="2">
<title>Hide your heart</title>
<note>this is for testing note, it is not required</note>
<quantity>1</quantity>
<price>9.90</price>
</item-2>
其余部分基本类似,比较需要注意的是item类型,它有attribute也有value。
<item index="2"> some tomatoes </item>
它的定义是
<xsd:element name="item" minOccurs="1" maxOccurs="1">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="index" type="xsd:unsignedLong" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
图片