20 June 2014

XML Validation Using XSD File


The example will show how to check the xml format validation by using XSD file.

XMLValidator.java
package org.metadata.core.xml;

import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

/**
 * A sample application which shows how to perform a
 * XML document validation.
 */
public class XMLValidator {
    public static void main(String[] args) {
        try {
            // define the type of schema - we use W3C:
            String schemaLang = "http://www.w3.org/2001/XMLSchema";

            // get validation driver:
            SchemaFactory factory = SchemaFactory.newInstance(schemaLang);

            // create schema by reading it from an XSD file:
            Schema schema = factory.newSchema(new StreamSource("resources/sample.xsd"));
            Validator validator = schema.newValidator();

            // at last perform validation:
            validator.validate(new StreamSource("resources/sample.xml"));
            System.out.println("Valid XML Format....");
        } catch (SAXException ex) {
            System.out.println("Invalid XML Format....");
            ex.printStackTrace();
        } catch (Exception ex) {
            System.out.println("Invalid XML Format....");
            ex.printStackTrace();
        }
    }
}

  1. Create a folder name with “resources” and set the folder into the classpath.
  2. Put the “sample.xml” and “sample.xsd” file into the “resources” folder.
  3. Make sure schema location in “sample.xml” file.

sample.xml
<?xml version="1.0" encoding="UTF-8"?>
<noties xsi:noNamespaceSchemaLocation="sample.xsd"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <notic>
      <line>
          <content>Mutu</content>
          <color>#0052415</color>
      </line>
      <line>
          <content>Zaw Than Oo</content>
          <color>red</color>
      </line>
      <line>
          <content>John Smith</content>
          <color>blue</color>
      </line>
      <line>
          <content>CycDemo</content>
          <color></color>
      </line>
  </notic>
</noties>

sample.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="noties">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="notic" minOccurs="1" maxOccurs="1" nillable="false">
          <xs:complexType>
            <xs:sequence>
            <xs:element name="line" minOccurs="1" maxOccurs="unbounded" nillable="false">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="content" minOccurs="1" maxOccurs="1" nillable="false">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:minLength value="1"/>
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
                <xs:element name="color" type="xs:string"/>
              </xs:sequence>
            </xs:complexType>
            </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Complete Package Structure



No comments:

Post a Comment

Like us on Facebook