I get exception while programatically validating xml file against a schema.

hi,
  I am trying to programatically validate a xml file against a schema. Please find the xml file, xsd file and code I wrote.
xml:
----
<?xml version="1.0" encoding = "UTF-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file://c:/TestSchema.xsd">
      <RequestPlan>
            <PCPID>1212121</PCPID>
            <PCPClientID>343434343</PCPClientID>
            <Name>Harish Kumar</Name>
      </RequestPlan>
      <RequestPlan>
            <PCPID>10128903</PCPID>
            <PCPClientID>59634032</PCPClientID>
            <Name>Raj</Name>
      </RequestPlan>
      <RequestPlan>
            <PCPID>34623412</PCPID>
            <PCPClientID>234235623412</PCPClientID>
            <Name>Mohan</Name>
      </RequestPlan>
</Root>


xsd:
----
<?xml version="1.0" encoding="UTF-8"?>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
            <xs:element name="Root">
                  <xs:complexType>
                        <xs:sequence>
                              <xs:element name="RequestPlan" type="ReqPl" maxOccurs="unbounded"/>
                        </xs:sequence>
                  </xs:complexType>
            </xs:element>
                  <xs:complexType name="ReqPl">
                        <xs:sequence>
                              <xs:element name="PCPID" type="xs:string"/>
                              <xs:element name="PCPClientID" type="xs:string"/>
                              <xs:element name="Name" type="xs:string"/>
                        </xs:sequence>
                  </xs:complexType>
      </xs:schema>
Client code:
-----

package com.axp.irsc;

import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.parsers.XMLParser;
import java.io.File;
import org.w3c.dom.Document;
import java.io.*;
import org.apache.axis.utils.XMLUtils;

public class SchemaValidator
{
  public SchemaValidator()
  {
  }

     public void validateSchema(String SchemaUrl, String strXml)
     {

          try
          {
              System.out.println("SchemaUrl" + SchemaUrl);
               //DOMParser domParser = new DOMParser();
               SAXParser domParser = new SAXParser();
               System.out.println("created object");
               String SchemaUrl="C:\\TestSchema.xsd";

               
               domParser.setFeature("http://xml.org/sax/features/validation",true);
               domParser.setFeature("http://apache.org/xml/features/validation/schema",true);
               domParser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);
               domParser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",SchemaUrl);
               
               Validator handler=new Validator();
               domParser.setErrorHandler(handler);  
               domParser.parse("c:\\IRSCData.xml");


               if(handler.validationError==true)
               {
                    handler.saxParseException.printStackTrace();
               }
               else
                    System.out.println("XML Document is valid");
             
               System.out.println("done the parse");

          }
          catch (Exception e)
          {
               e.printStackTrace();
          }
     }
 
     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          
          {          
          }    
     }


     public static void main(String str[]) throws Exception
     {
          System.out.println("Validating XML");
          System.out.println("PARSE XML/XSD DOCUMENTS");
          SchemaValidator validator = new SchemaValidator();
          validator.validateSchema();


     }
}


I get following error:
     org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Name'.
     at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:232)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:173)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:362)

I get no error if I remove the following line from the above code
   domParser.setFeature("http://xml.org/sax/features/validation",true);

Kindly tell me what the above line is doing in the code also the solution.



harish_alwalaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ramazanyichCommented:
domParser.setFeature("http://xml.org/sax/features/validation",true);
says to Parser to perform validation of XML against provided XSD schema.
Your schema file is correct. But in  xml file change attribute xsi:noNamespaceSchemaLocation. You root element should be:
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="c:\TestSchema.xsd">
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
harish_alwalaAuthor Commented:
thanks for the solution.
I have one other query on the same. Is it mandatory to write "<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="c:\TestSchema.xsd">" in xml, because I have to generate xml string progrmatically and validate against schema.


The same code written above is working even if I dont specify "noNamespaceSchemaLocation" in xml file. Is the code really validating against schema even i dont specify the "noNamespaceSchemaLocation" in xml file??
Please clarify.

0
ramazanyichCommented:
yes it should, because you use explicit call:
     domParser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",SchemaUrl);

You can check it by putting some mistake in your xml, eg. add non-specified element into your xml.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.