XML
validation by using XML Schema |
This document describes how to
validate an XML document with an XML schema. Although this is a brief example, you should be able to execute this example. For this example I created a 'person' data object which includes structure checking, range checking for the age value, regular expressions for the email value and default values for the country. The example consist of: |
|
For any questions contact me at java @ groot . cc (remove the spaces in the e-mail address) Rik de Groot . |
Requirements |
|
Java code - XsdTestErrorHandler.java |
package
cc.groot.xsd; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; /** * @author Rik de Groot */ public class XsdTestErrorHandler implements ErrorHandler { /** * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException) */ public void warning(SAXParseException anException) throws SAXException { System.out.println("[warning] " + anException); } /** * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException) */ public void error(SAXParseException anException) throws SAXException { System.out.println("[error] " + anException); } /** * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException) */ public void fatalError(SAXParseException anException) throws SAXException { System.out.println("[fatal error] " + anException); } } |
Java code - XsdTest.java |
package
cc.groot.xsd; import java.io.IOException; import org.apache.xerces.parsers.DOMParser; import org.w3c.dom.Document; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; /** * @author Rik de Groot */ class XsdTest { public static void main(String args[]) { XsdTestErrorHandler myErrorHandler = new XsdTestErrorHandler(); DOMParser myParser = new DOMParser(); Document myDocument = null; // Setup the a validating parser with an xsd. try { //Validate the document and report validity errors. myParser.setFeature("http://xml.org/sax/features/validation", true); //Turn on XML Schema validation by inserting XML Schema // validator in the pipeline. myParser.setFeature( "http://apache.org/xml/features/validation/schema",true); //Set the external schema location. myParser.setProperty( "http://apache.org/xml/properties/schema/external-schemaLocation", "person person.xsd"); myParser.setErrorHandler(myErrorHandler); } catch (SAXNotRecognizedException e) { System.out.print("Unrecognized property: "); } catch (SAXNotSupportedException e) { System.out.print("Unsupported property: "); } // Parse an XML document try { myParser.parse("person.xml"); myDocument = myParser.getDocument(); } catch (IOException ie) { System.out.println("Could not read file."); } catch (SAXException e) { System.out.print("Could not create Document: "); System.out.println(e.getMessage()); } //Put here your code for getting data from the document System.out.println("validation ready"); } } |
XML
-person.xsd |
<?xml version="1.0"
encoding="ISO-8859-1" ?> <xsd:schema xmlns="person" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="person" elementFormDefault="qualified"> <xsd:element name="company"> <xsd:complexType> <xsd:sequence minOccurs="1"> <xsd:element name="person" type="person_type"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="person_type"> <xsd:attribute name="name" type="xsd:string" use="required"/> <xsd:attribute name="age" type="age_type" use="required"/> <xsd:attribute name="country" type="xsd:string" default="Netherlands"/> <xsd:attribute name="email" type="email_type"/> </xsd:complexType> <xsd:simpleType name="age_type"> <xsd:restriction base="xsd:integer"> <xsd:minExclusive value="0"/> <xsd:maxInclusive value="200"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="email_type"> <xsd:restriction base="xsd:string"> <xsd:pattern value=".+@.+"/> </xsd:restriction> </xsd:simpleType> </xsd:schema> |
XML -person.xml |
<?xml
version="1.0" encoding="ISO-8859-1" ?> <company xmlns="person" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="person person.xsd"> <person name="Rik de Groot" age="29" email="rik@company.com"/> </company> |
Examples |
Normal |
validation ready. |
Invalid XML Syntax
- </company> is change to </companyx> |
[fatal error]
org.xml.sax.SAXParseException: The end-tag for element type "company"
must end with a '>' delimiter. Could not create Document: The end-tag for element type "company" must end with a '>' delimiter. validation ready. |
Value out of range -
age="29" is changed to age="329" |
[error]
org.xml.sax.SAXParseException: cvc-maxInclusive-valid: Value '329' is
not facet-valid with respect to maxInclusive '200'. [error] org.xml.sax.SAXParseException: cvc-attribute.3: The value '329' of attribute 'age' on element 'person' is not valid with respect to its type. validation ready. |
Invalid Schema syntax -
minOccur="1" if changed to minOccurrs="1" |
[error]
org.xml.sax.SAXParseException: s4s-att-not-allowed: Attribute
'minOccurrs' cannot appear in element 'sequence'. validation ready. |
Invalid Schema syntax -
<xsd:element name="person" type="person_type" is changed to
<xsd:element name="person" type="person1_type" |
[error]
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name
'person1_type' to a(n) type definition component. validation ready. |
Resources |
|