Sunday, June 21, 2015

Validate XML With Referenced XSD File Using SAX Parser

Previously we learn how to validated a XML file with reference or internal schema using DOM parser. Here we do the same thing but using SAX parser.

Step 1:

  Create a  SAXParserFactory object.
      SAXParserFactory saxFactory = SAXParserFactory.newInstance();  

Step 2:
  Set SAXParserFactory object as NameSpaceAware true.
      saxFactory.setNamespaceAware(true);

Step 3:
  Set SAXParserFactory object as Validating true.
      saxFactory.setValidating(true);

Step 4:
   Set SAXParser Property.
      parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                    "http://www.w3.org/2001/XMLSchema");


Step 5:
  Set ErrorHandler to XMLReader object.
                xmlReader.setErrorHandler(
                    new ErrorHandler() {
                        @Override
                        public void warning(SAXParseException e) throws SAXException {
                            System.out.println("WARNING: " + e.getMessage()); // do nothing
                        }
                       
                        @Override
                        public void error(SAXParseException e) throws SAXException {
                            System.out.println("ERROR: " + e.getMessage());
                            throw e;
                        }
                       
                        @Override
                        public void fatalError(SAXParseException e) throws SAXException {
                            System.out.println("FATAL: " + e.getMessage());
                            throw e;
                        }
                    });


Step 6:
  Parse XML file .
    xmlReader.parse("file.xml");

Our complete code looks like that:

package SAXParser;

import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;


public class SAXParserValidation {

    public static void main(String[] args) {
        try {
            SAXParserFactory saxFactory = SAXParserFactory.newInstance();
            saxFactory.setValidating(true);
            saxFactory.setNamespaceAware(true);
            SAXParser parser = saxFactory.newSAXParser();
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                    "http://www.w3.org/2001/XMLSchema");
           
            XMLReader xmlReader = parser.getXMLReader();
            xmlReader.setErrorHandler(
                    new ErrorHandler() {
                        @Override
                        public void warning(SAXParseException e) throws SAXException {
                            System.out.println("WARNING: " + e.getMessage()); // do nothing
                        }
                       
                        @Override
                        public void error(SAXParseException e) throws SAXException {
                            System.out.println("ERROR: " + e.getMessage());
                            throw e;
                        }
                       
                        @Override
                        public void fatalError(SAXParseException e) throws SAXException {
                            System.out.println("FATAL: " + e.getMessage());
                            throw e;
                        }
                    });
            xmlReader.parse("file.xml");
        } catch (ParserConfigurationException | SAXException | IOException e) {
            System.out.println("!!!!!!!!!!!!!!!Error!!!!!!!!!!!!" + e.getMessage());
        }
    }
   


 XML File :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<students  xsi:noNamespaceSchemaLocation="file.xsd"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <student id="1">
    <fname>TestFirstName</fname>
    <lname>TestLastName</lname>
    <sectionname rollno="1">A</sectionname>
  </student>
</students>




XML Schema :

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="students" type="studentsType"/>
  <xs:complexType name="sectionnameType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:byte" name="rollno"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="studentType">
    <xs:sequence>
      <xs:element type="xs:string" name="fname"/>
      <xs:element type="xs:string" name="lname"/>
      <xs:element type="sectionnameType" name="sectionname"/>
    </xs:sequence>
    <xs:attribute type="xs:byte" name="id"/>
  </xs:complexType>
  <xs:complexType name="studentsType">
    <xs:sequence>
      <xs:element type="studentType" name="student"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

No comments:

Post a Comment