1
Validating XMLFile
  • The code itself is short
  • public boolean validateXmlFile() throws Exception
    {
    XmlValidator xmlValidator = new XmlValidator();

    String schemaUrl = "file:\\\\C:\\Program Files\\Apache Group\\jakarta-tomcat-4.1.27\\webapps\\cs554project\\include\\credit-card-payment.xsd";
    String xmlFile = "file:\\\\C:\\Program Files\\Apache Group\\jakarta-tomcat-4.1.27\\webapps\\cs554project\\WEB-INF\\paymentfile.xml";

    return xmlValidator.validateSchema(schemaUrl,xmlFile); //specify the Schema file and file to be validated
  • }
  • package edu.umkc.cs554.project.common.utilclasses;
     
  • // I have used Oracle's XDK Parser

  • import oracle.xml.schemavalidator.XSDValidator;
    import oracle.xml.parser.schema.XSDException;
    import oracle.xml.parser.schema.XMLSchema;
    import oracle.xml.parser.schema.XSDBuilder;
    import oracle.xml.parser.v2.XMLError;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    import org.xml.sax.helpers.DefaultHandler;
    import java.net.URL;

    public class XmlValidator
    {

    public boolean validateSchema(String SchemaUrl, String XmlDocumentUrl)
    {
    boolean flag = false;

    try
    {
    XSDValidator xsdValidator=new XSDValidator(); // Oracle's implementation class in xschema.jar


    XSDBuilder builder = new XSDBuilder(); // Oracle's implementation class in xschema.jar

    System.out.println("SchemaUrl : " + SchemaUrl);
    System.out.println("XmlDocumentUrl" + XmlDocumentUrl);

    URL url = new URL(SchemaUrl);

    System.out.println("XML schema validation started");

    XMLSchema schemadoc = (XMLSchema)builder.build(url); // Oracle's implementation class in xschema.jar

    xsdValidator.setSchema(schemadoc); //first validate the schema

    System.out.println("XML schema validation completed");

    Validator handler=new Validator();
    XMLError xmlError=new XMLError();
    xmlError.setErrorHandler(handler);
    xsdValidator.setError(xmlError);
  • xsdValidator.validate(new URL(XmlDocumentUrl));// validate the XML schema against the XML file

    System.out.println("XML Document validation started");

    if(handler.validationError==true)
    {
    System.out.println("XML Document has Error:"+handler.validationError+""+handler.saxParseException.getMessage());
    }
    else
    {
    flag = true;
    System.out.println("XML Document is valid");
    }

    }
    catch(java.io.IOException ioe)
    {
    System.out.println("IOException "+ioe.getMessage());
    }
    catch (SAXException e)
    {
    System.out.println("SAXException "+e.getMessage());
    }
    catch (XSDException e)
    {
    System.out.println("SAXException "+e.getMessage());
    }

    return flag;
    }

    private class Validator extends DefaultHandler
    {
    public boolean validationError = false;
    public SAXParseException saxParseException=null;

    public void error(SAXParseException exception) throws SAXException
    {
    validationError = true;
    saxParseException=exception;
    }
    public void fatalError(SAXParseException exception) throws SAXException
    {
    validationError = true;
    saxParseException=exception;
    }
    public void warning(SAXParseException exception) throws SAXException
    {
    }
    }

    }

 

  • XML Schema
  • <?xml version="1.0" encoding="utf-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="creditcardpayment">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="paymentamount" type="xs:string"/>
    <xs:element name="paymentdate" type="xs:string"/>
    <xs:element name="paymentdesc" type="xs:string"/>
    <xs:element ref="customer" minOccurs="1" maxOccurs="1"/>
    <xs:element ref="creditcardinfo" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
    <xs:attribute name="payerid" type="xs:positiveInteger"/>
    </xs:complexType>
    </xs:element>

    <xs:element name="customer">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    <xs:element ref="address" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
    <xs:attribute name="customerid" type="xs:positiveInteger"/>
    </xs:complexType>
    </xs:element>

    <xs:element name="address">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="street1" type="xs:string"/>
    <xs:element name="street2" type="xs:string"/>
    <xs:element name="city" type="xs:string"/>
    <xs:element name="state" type="xs:string"/>
    <xs:element name="zip" type="xs:string"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    <xs:element name="creditcardinfo">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="cardnumber" type="xs:string"/>
    <xs:element name="cardtype" type="xs:string"/>
    <xs:element name="expirydate" type="xs:string"/>
    <xs:element name="securitycode" type="xs:string"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    </xs:schema>

 

  • XML File
  • <?xml version="1.0" encoding="UTF-8"?>
    <creditcardpayment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" payerid="123456789" > <paymentamount>800</paymentamount>
    <paymentdate>2004/04/15</paymentdate>
    <paymentdesc>This is payment for student fee</paymentdesc>
    <customer>
    <firstname>ZAHEER</firstname>
    <lastname>SHAIK</lastname>
    <address>
    <street1>7575 W 106 th St</street1>
    <street2>Apt 297</street2>
    <city>Overland park</city>
    <state>KS</state>
    <zip>66212</zip>
    </address>
    </customer>
    <creditcardinfo><cardnumber>1234567891234567</cardnumber>
    <cardtype>Discover</cardtype>
    <expirydate>12/31/2004</expirydate>
    <securitycode>123</securitycode>
    </creditcardinfo>
    </creditcardpayment>

     
  • References :

    http://java.sun.com/webservices/docs/1.3/tutorial/doc/IntroWS6.html#wp72426