DOM4J使用schema验证XML

package com.tlq.dom4j;
import java.io.InputStream;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
  
public class Dom4jTest {   
 
    /**  
     * @param args  
     */  
    public static void main(String[] args) {   
        Dom4jTest dt = new Dom4jTest();   
        try {   
         //Document doc = dt.read("/org/soft/resource/shiporder.xml");  
         //直接传给它地址
         //Document doc = dt.read("E:/myworkspace/Dom4jTest_XSD/src/com/tlq/dom4j/shiporder2.xml");   
         Document doc = dt.read("/com/tlq/dom4j/shiporder.xml");   
            Element element = dt.getRootElement(doc);   
            System.out.println(element.getPath());
        } catch (DocumentException e) {   
            e.printStackTrace();   
        } catch (SAXException e) {   
            e.printStackTrace();   
        }   
  
    }   
  
    public Document read(String fileName) throws DocumentException,   
            SAXException {   
        SAXReader reader = new SAXReader(true);   
  
        EntityResolver resolver = new EntityResolver() {   
            public InputSource resolveEntity(String publicId, String systemId) {   
             //InputStream in = reader.read("/aa/shiporder3.xsd");   
                InputStream in = this.getClass().getResourceAsStream("/com/tlq/dom4j/shiporder.xsd");   
                return new InputSource(in);   
            }   
        };   
        reader.setEntityResolver(resolver);   
        reader.setFeature("http://xml.org/sax/features/validation", true);   
        reader.setFeature("http://apache.org/xml/features/validation/schema", true);   
        reader.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);   
        // reader.setProperty(   
        // "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",   
        // "shiporder.xsd");   
   //获得一个路役
       // Document doc = reader.read("E:/myworkspace/Dom4jTest_XSD/src/com/tlq/dom4j/shiporder2.xml");   
       //Document doc = reader.read(fileName);   
        Document doc = reader.read(this.getClass().getResourceAsStream(fileName));   
  
        return doc;   
    }   
  
    public Element getRootElement(Document doc) {   
        return doc.getRootElement();   
    }   
}  

 

 

 

 

XSD 文件

<?xml version="1.0" encoding="UTF-8" ?>  
  
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">      
    <xs:element name="name" type="xs:string" />  
    <xs:element name="address" type="xs:string" />  
    <xs:element name="city" type="xs:string" />  
    <xs:element name="country" type="xs:string" />  
    <xs:element name="title" type="xs:string" />  
    <xs:element name="note" type="xs:string" />  
    <xs:element name="quantity" type="xs:positiveInteger" />  
    <xs:element name="price" type="xs:decimal" />  
  
    <xs:attribute name="orderid">  
        <xs:simpleType>  
            <xs:restriction base="xs:string">  
                <xs:pattern value="[0-9]{6}"/>  
            </xs:restriction>  
        </xs:simpleType>  
    </xs:attribute>  
  
    <xs:element name="shiporder">  
        <xs:complexType>  
  
            <xs:sequence>  
                <xs:element name="orderperson" type="xs:string" />  
  
                <xs:element name="shipto">  
                    <xs:complexType>  
                        <xs:sequence>  
                            <xs:element ref="name" />  
                            <xs:element ref="address" />  
                            <xs:element ref="city" />  
                            <xs:element ref="country" />  
                        </xs:sequence>  
                    </xs:complexType>  
                </xs:element>  
  
                <xs:element name="item" maxOccurs="unbounded">  
                    <xs:complexType>  
                        <xs:sequence>  
                            <xs:element ref="title" />  
                            <xs:element ref="note" minOccurs="0" />  
                            <xs:element ref="quantity" />  
                            <xs:element ref="price" />  
                        </xs:sequence>  
                    </xs:complexType>  
                </xs:element>  
  
            </xs:sequence>  
            <xs:attribute ref="orderid" use="required" />  
        </xs:complexType>  
    </xs:element>  
</xs:schema>

 

 

XML文件

<?xml version="1.0" encoding="UTF-8"?>  
  
<shiporder orderid="889923"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:noNamespaceSchemaLocation="shiporder.xsd">  
    <orderperson>George Bush</orderperson>  
    <shipto>  
        <name>John Adams</name>  
        <address>Oxford Street</address>  
        <city>London</city>  
        <country>UK</country>  
    </shipto>  
    <item>  
        <title>Empire Burlesque</title>  
        <note>Special Edition</note>  
        <quantity>1</quantity>  
        <price>10.90</price>  
    </item>  
    <item>  
        <title>Hide your heart</title>  
        <quantity>1</quantity>  
        <price>9.90</price>  
    </item>  
</shiporder>

你可能感兴趣的:(schema)