背景:做的數據同步框架,數據同步種類通過xml配置文件添加。為了系統的穩定性,我們只能認為將來寫這個運行配置xml的人是一個傻瓜,那么對xml格式校驗就很重要。
通過dom4j,是可以完成對xml格式的校驗。但是代碼實現冗長,不便閱讀和維護。而且后面校驗格式一旦修改,代碼方面的修改就是牽一發而動全身。看着實在蛋疼,於是為了“偷懶”,我棄用了別人實現的dom4j校驗xml數據格式的方法。
通過在網絡上查閱,發現對xml格式校驗的常用方法:①DTD語法;②xml schema語法。
下面是關於DTD和Schema的對比(摘自:http://www.oseye.net/user/kevin/blog/283)
DTD 的局限性
DTD不遵守XML語法(寫XML文檔實例時候用一種語法,寫DTD的時候用另外一種語法)
DTD數據類型有限(與數據庫數據類型不一致)
DTD不可擴展
DTD不支持命名空間(命名沖突)
Schema的新特性
Schema基於XML語法
Schema可以用能處理XML文檔的工具處理
Schema大大擴充了數據類型,可以自定義數據類型
Schema支持元素的繼承—Object-Oriented’
Schema支持屬性組
因schema和dtd,自己以前都沒怎么深入研究過。看了很多人都說schema都將取代dtd,因此站在前輩的肩膀上就直接使用了schema。希望古人誠不坑我!
xml schema 的學習,建議去官方網站學習。傳送門:http://w3school.com.cn/schema/index.asp
一位前輩寫了實際有用的技術分享,傳送門:http://bbs.esnai.com/forum.php?mod=viewthread&action=printable&tid=1568142
下面上正菜:
我的xml配置文件
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <ships>
4
5 <ship>
6 <id>1</id>
7 <name>1</name>
8 <cron>1</cron>
9 <sources>
10 <source></source>
11 <source></source>
12 <source></source>
13 </sources>
14 <aims>
15 <aim></aim>
16 <aim></aim>
17 <aim></aim>
18 </aims>
19 <type>1</type>
20 <handles>
21 <handle>
22 <head></head>
23 <body></body>
24 <end></end>
25 </handle>
26 </handles>
27 </ship>
28
29 <ship>
30 <id>1</id>
31 <name>1</name>
32 <cron>1</cron>
33 <sources>
34 <source></source>
35 <source></source>
36 <source></source>
37 </sources>
38 <aims>
39 <aim></aim>
40 <aim></aim>
41 <aim></aim>
42 </aims>
43 <type>1</type>
44 <handles>
45 <handle>
46 <head></head>
47 <body></body>
48 <end></end>
49 </handle>
50 </handles>
51 </ship>
52
53 <ship>
54 <id>1</id>
55 <name>1</name>
56 <cron>1</cron>
57 <sources>
58 <source></source>
59 <source></source>
60 <source></source>
61 </sources>
62 <aims>
63 <aim></aim>
64 <aim></aim>
65 <aim></aim>
66 </aims>
67 <type>1</type>
68 <handles>
69 <handle>
70 <head></head>
71 <body></body>
72 <end></end>
73 </handle>
74 </handles>
75 </ship>
76
77 <ship>
78 <id>1</id>
79 <name>1</name>
80 <cron>1</cron>
81 <sources>
82 <source></source>
83 <source></source>
84 <source></source>
85 </sources>
86 <aims>
87 <aim></aim>
88 <aim></aim>
89 <aim></aim>
90 </aims>
91 <type>1</type>
92 <handles>
93 <handle>
94 <head></head>
95 <body></body>
96 <end></end>
97 </handle>
98 </handles>
99 </ship>
100
101 </ships>
我的xsd檢驗規范文件
1 <?xml version="1.0" encoding="UTF-8"?>
2 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3
4 <!-- 頂層元素規范定義-->
5 <xs:element name="ships">
6 <xs:complexType ><!-- 復雜元素定義-->
7 <xs:sequence><!-- 要求子元素必須按順序出現。每個子元素可出現 0 到任意次數-->
8 <!-- ship元素標簽列表定義-->
9 <xs:element name="ship" type="ship_type" minOccurs="0" maxOccurs="unbounded"/>
10 <!-- 注意 上述type 說明標簽ship的結構 具體結構定義看下面-->
11 </xs:sequence>
12 </xs:complexType>
13 </xs:element>
14
15 <!-- 第二層節點定義 -->
16 <xs:complexType name="ship_type">
17 <xs:sequence>
18 <xs:element name="id" type="xs:string"></xs:element><!-- 簡單元素節點定義 -->
19 <xs:element name="name" type="xs:string"></xs:element>
20 <xs:element name="cron" type="xs:string"></xs:element>
21 <xs:element name="sources"><!-- 簡單list節點定義-->
22 <xs:complexType>
23 <xs:sequence>
24 <xs:element name="source" minOccurs="0" maxOccurs="unbounded"/>
25 </xs:sequence>
26 </xs:complexType>
27 </xs:element>
28 <xs:element name="aims">
29 <xs:complexType>
30 <xs:sequence>
31 <xs:element name="aim" minOccurs="0" maxOccurs="unbounded"/>
32 </xs:sequence>
33 </xs:complexType>
34 </xs:element>
35 <xs:element name="type" type="xs:string"></xs:element>
36 <xs:element name="handles"><!--handles標簽子節點定義 -->
37 <xs:complexType>
38 <xs:sequence>
39 <xs:element name="handle" type="handle_type" minOccurs="0" maxOccurs="unbounded"></xs:element>
40 </xs:sequence>
41 </xs:complexType>
42 </xs:element>
43 </xs:sequence>
44 </xs:complexType>
45 <!-- 第三層標簽節點格式定義 -->
46 <xs:complexType name="handle_type">
47 <xs:sequence>
48 <xs:element name="head" type="xs:string"></xs:element>
49 <xs:element name="body" type="xs:string"></xs:element>
50 <xs:element name="end" type="xs:string"></xs:element>
51 </xs:sequence>
52 </xs:complexType>
53 </xs:schema>
xml文件檢驗工具(非個人開發,網上直接copy,經測試可用-校驗工具尚需自己完善,但是架子ok了。傳送門:http://www.cnblogs.com/qqzy168/p/3394312.html)
1 package xml.util; 2
3 import org.xml.sax.SAXException; 4
5 import javax.xml.transform.Source; 6 import javax.xml.transform.stream.StreamSource; 7 import javax.xml.validation.Schema; 8 import javax.xml.validation.SchemaFactory; 9 import javax.xml.validation.Validator; 10 import java.io.File; 11 import java.io.IOException; 12
13 /**
14 * Created by robin on 2016/6/12. 15 * 16 * @author robin 17 */
18 public class ValidateXML { 19 private ValidateXML() { 20
21 } 22
23 public static boolean validateXml(String xsdPath, String xmlPath) 24 throws SAXException, IOException { 25 // 建立schema工廠
26 SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); 27 // 建立驗證文檔文件對象,利用此文件對象所封裝的文件進行schema驗證
28 File schemaFile = new File(xsdPath); 29 // 利用schema工廠,接收驗證文檔文件對象生成Schema對象
30 Schema schema = schemaFactory.newSchema(schemaFile); 31 // 通過Schema產生針對於此Schema的驗證器,利用schenaFile進行驗證
32 Validator validator = schema.newValidator(); 33 // 得到驗證的數據源
34 Source source = new StreamSource(xmlPath); 35 // 開始驗證,成功輸出success!!!,失敗輸出fail
36 validator.validate(source); 37
38 return true; 39 } 40 }
測試demo
1 package xml.xmlDemo; 2
3 import org.xml.sax.SAXException; 4 import xml.util.ValidateXML; 5
6 import java.io.IOException; 7
8 /**
9 * Created by robin on 2016/6/12. 10 * 11 * @author robin 12 */
13 public class Test { 14
15 public static void main(String args[]) throws IOException, SAXException { 16 System.out.print(ValidateXML.validateXml("src\\xml\\xmlDemo\\ships.xsd", "src\\xml\\xmlDemo\\ships.xml")); 17 } 18 }
schema強大之處還在於,通過xsd,可以讓寫配置文件的人,在編寫xml就知道自己寫的對不對,哪里寫錯了。
將ship標簽改為<ship xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ships.xsd"> ....</ship>
正菜就這么點,自己也是剛開始玩schema,歡迎大伙前來指正或是交流。
后記:xml schema 進行xml格式校驗還有很多強大之處。尚待各位去摸索...
以上內容,都經過本人實踐驗證過。若轉發,請在標題上標記[轉],並注明原文鏈接:http://www.cnblogs.com/robinjava77/p/5578224.html ,作者名稱:robin。並在文章最后一行附上本句話。否則,作者保留追究的權利。

