SaxXmlXsValidation.java - SAX Parser with XS Validation

Q

How to parse an XML file with XS (XML Schema) validation using the SAX API?

✍: FYIcenter

A

if you want to parse an XML file with XS (XML Schema) validation using the SAX API, you can follow these suggestions:

1. Set the validation flag to true on SAXParserFactory object:

      SAXParserFactory f = SAXParserFactory.newInstance();
    f.setValidating(Boolean.parseBoolean(args[1]));

2. Turn Schema Validation on and Namespace Awareness on:

      f.setNamespaceAware(true);
    f.setFeature("http://apache.org/xml/features/validation/schema",true);

3. Provide the XML file with XML Schema location (User.xsd) provided, UserXsdError.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Copyright (c) 2017 FYIcenter.com -->
<fyi:User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:fyi="http://fyicenter.com"
  xsi:schemaLocation="http://fyicenter.com User.xsd">
    <fyi:ID>ONE</fyi:ID>
    <fyi:Name>Frank Y. Ivy</fyi:Name>
    <fyi:BirthDate>1970-01-01+00:01</fyi:BirthDate>
    <fyi:Sex>  Male</fyi:Sex>
</fyi:User>

Here is a complete example of parsing an XML with DTD validation using SAX API, SaxXmlXsValidation.java:

// Copyright (c) 2017 FYIcenter.com
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import java.io.*;

public class SaxXmlXsValidation extends DefaultHandler {
   static String dot = "............................................................";
   static int l = 0;
   public static void main(String[] args) throws Exception {
      SAXParserFactory f = SAXParserFactory.newInstance();
      f.setNamespaceAware(true);
    f.setValidating(Boolean.parseBoolean(args[1]));
    f.setFeature("http://apache.org/xml/features/validation/schema",true);
      SAXParser p = f.newSAXParser();
      System.out.println("Parser class: "+p.getClass().getName());

    p.parse(new File(args[0]), new SaxXmlXsValidation());
   }
   public void startElement(String uri, String lName, String qName, Attributes atts) {
      l++;
      System.out.print("\n"+dot.substring(0,l)+lName+qName);
   }
   public void endElement(String uri, String lName, String qName) {
      l--;
   }
   public void characters(char[] ch, int start, int length) {
      System.out.print("("+(new String(ch,start,length))+")");
   }
   public void error(SAXParseException e) throws SAXException {
      System.out.println("\nError: "+e.toString()); 
   }
}

Compile and run the example program, SaxXmlXsValidation.java:

>\fyicenter\jdk-1.8.0\bin\javac SaxXmlXsValidation.java

>\fyicenter\jdk-1.8.0\bin\java SaxXmlXsValidation UserXsdError.xml true

Parser class: com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl

.Userfyi:User
..IDfyi:ID(ONE)
Error: org.xml.sax.SAXParseException; systemId: file:UserXsdError.xml; lineNumber: 6; columnNumber: 25; 
   cvc-datatype-valid.1.2.1: 'ONE' is not a valid value for 'integer'.
Error: org.xml.sax.SAXParseException; systemId: file:UserXsdError.xml; lineNumber: 6; columnNumber: 25; 
   cvc-type.3.1.3: The value 'ONE' of element 'fyi:ID' is not valid.

..Namefyi:Name(Frank Y. Ivy)
..BirthDatefyi:BirthDate(1970-01-01+00:01)
Error: org.xml.sax.SAXParseException; systemId: file:UserXsdError.xml; lineNumber: 8; columnNumber: 52; 
   cvc-datatype-valid.1.2.1: '1970-01-01+00:01' is not a valid value for 'dateTime'.
Error: org.xml.sax.SAXParseException; systemId: file:UserXsdError.xml; lineNumber: 8; columnNumber: 52; 
   cvc-type.3.1.3: The value '1970-01-01+00:01' of element 'fyi:BirthDate' is not valid.

..Sexfyi:Sex(Male)

If you run it again with "false", you will not see any validation errors:

>\fyicenter\jdk-1.8.0\bin\java SaxXmlXsValidation UserXsdError.xml false

Parser class: com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl

.Userfyi:User
..IDfyi:ID(ONE)
..Namefyi:Name(Frank Y. Ivy)
..BirthDatefyi:BirthDate(1970-01-01+00:01)
..Sexfyi:Sex(Male)

The XML Schema file used in the above test is, User.xsd:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Copyright (c) 2017 FYIcenter.com -->
<xsd:schema
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:fyi="http://fyicenter.com"
   targetNamespace="http://fyicenter.com"
   elementFormDefault="qualified">

  <xsd:element name="User">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="ID" type="xsd:integer"/>
        <xsd:element name="Name" type="xsd:string"/>
        <xsd:element name="BirthDate" type="xsd:dateTime"/>
        <xsd:element name="Sex" type="fyi:sexType"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:simpleType name="sexType">
    <xsd:restriction base="xsd:NMTOKEN">
      <xsd:enumeration value="Male"/>
      <xsd:enumeration value="Femal"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>

 

Using Apache Xerces DOM Sample Programs

SaxXmlDtdValidation.java - SAX Parser with DTD Validation

Using XML SAX API with Apache Xerces

⇑⇑ FAQ for Apache Xerces XML Parser

2017-12-04, 1807🔥, 0💬