Wednesday, May 28, 2008

XML Schema Validation and importing different encoding schema

Following is a simple code which shows importing an different encoding xml schema into a different encoding xml schema. In the following example Book.xsd imports Age.xsd which has UTF-16 encoding. Following example also shows XML Schema Validation

XMLValidationSample.java
import java.io.IOException;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXParseException;
import javax.xml.parsers.ParserConfigurationException;
public class XMLValidationSample {
public static void main(String args[]) {
try{
String book = "Book.xsd", first = "first.xsd", last = "last.xsd", age = "age.xsd";
String[] schemas = {book, first, last, age};
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware( true);
factory.setValidating( true);
SAXParser parser = factory.newSAXParser();
parser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
parser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaSource",
schemas);
XMLReader reader = parser.getXMLReader();
reader.parse( new InputSource( "Book.xml"));
}catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace (); }
catch (IOException e) {
e.printStackTrace (); }
catch(ParserConfigurationException e){
e.printStackTrace (); }
}
}
Book.xml

<?xml version="1.0" encoding="UTF-8"?>
<my:book xmlns:my="http://www.person.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.person.com Book.xsd">

<person>
<firstd><first>Johnthan</first></firstd>
<lastd><last>Schewatz</last></lastd>
<aged><age>38</age></aged>
</person>

<person>
<firstd><first>Bill</first></firstd>
<lastd><last>Gates</last></lastd>
<aged><age>46</age></aged>
</person>

<person>
<firstd><first>Steve</first></firstd>
<lastd><last>Jobs</last></lastd>
<aged><age>40</age></aged>
</person>

</my:book>


Book.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.person.com" xmlns="http://www.person.com" xmlns:f="http://www.first.com" xmlns:l="http://www.last.com" xmlns:a="http://www.age.com" elementFormDefault="unqualified">
<xsd:import namespace="http://www.first.com" schemaLocation="first.xsd"/>

<xsd:import namespace="http://www.last.com" schemaLocation="last.xsd"/>
<xsd:import namespace="http://www.age.com" schemaLocation="age.xsd"/>
<xsd:element name="book">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="person" type="persontype" maxOccurs="unbounded"/> </xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="persontype">
<xsd:sequence>
<xsd:element name="firstd" type="f:firstType"/>
<xsd:element name="lastd" type="l:lastType"/>
<xsd:element name="aged" type="a:ageType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

first.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.first.com" xmlns="http://www.first.com" elementFormDefault="unqualified">
<xsd:complexType name="firstType">
<xsd:sequence>
<xsd:element name ="first" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

last.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.last.com" xmlns="http://www.last.com" elementFormDefault="unqualified">
<xsd:complexType name="lastType">
<xsd:sequence>
<xsd:element name ="last" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

age.xsd

<?xml version="1.0" encoding="UTF-16" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.age.com" xmlns="http://www.age.com" elementFormDefault="unqualified">
<xsd:complexType name="ageType">
<xsd:sequence>
<xsd:element name ="age" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>