DOM4J使用schema验证XML文件

本文的jar包及版本dom4j-1.6.1.jar,jaxen-1.1-beta-6.jar,xercesImpl-2.6.2.jar

Schema文件org/soft/resource/shiporder.xsd.
<?xml version="1.0" encoding="ISO-8859-1" ?>

<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文件org/soft/resource/shiporder.xml.
<?xml version="1.0" encoding="ISO-8859-1"?>

<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>


package org.soft.test;

import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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 {

	/** log */
	protected static final Log log = LogFactory.getLog(Dom4jTest.class);

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Dom4jTest dt = new Dom4jTest();
		try {
			Document doc = dt.read("/org/soft/resource/shiporder.xml");
			Element element = dt.getRootElement(doc);
			log.info(element.getPath());
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			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 = this.getClass().getResourceAsStream(
						"/org/soft/resource/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(this.getClass().getResourceAsStream(fileName));

		return doc;
	}

	public Element getRootElement(Document doc) {
		return doc.getRootElement();
	}
}

你可能感兴趣的:(java,apache,xml)