• Post Reply Bookmark Topic Watch Topic
  • New Topic

reg xml validation when i pass xml and the schema that is not used in xml  RSS feed

anish reddy
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am developing xml business component which validates xml according to schema/dtd.

Here is my code which does validation.

* create a Stream source for all schemas
* @param pSchema
* @return StreamSource[]
public StreamSource[] getStreamSource(String... pSchema) {
StreamSource[] lstStreamSource = new StreamSource[pSchema.length];
for (int i = 0; i < pSchema.length; i++) {
StreamSource lStreamSource = new StreamSource(pSchema[i]);
lstStreamSource[i] = lStreamSource;

return lstStreamSource;

* Validating xml doc with their schema
* @param pFileXmlOne
* @param pFileXmlTwo
* @param pValidate
* @param pSchemaNameOne
* @param pSchemaTwo
* @return boolean
public boolean performValidation(String pFileXmlOne, String... pSchemaNames)
throws Exception {

logger.log(Level.INFO, "in performValidation");

SchemaFactory factory = SchemaFactory

Source[] lStreamSources = null;

Schema schema = null;

Validator lValidator = null;

Source lSource = new StreamSource(pFileXmlOne);

// split schema string by delimiter

lStreamSources = getStreamSource(pSchemaNames);
logger.log(Level.INFO, myClassName + "total no of schemas"
+ lStreamSources.length);
try {
schema = factory.newSchema(lStreamSources);
} catch (SAXException e) {
// TODO Auto-generated catch block
logger.log(Level.INFO, myClassName
+ "sax exception in creating schema" + e.getMessage());
throw new SAXException(e.getMessage());
logger.log(Level.INFO, myClassName
+ " before creating validator object");
lValidator = schema.newValidator();
.log(Level.INFO, myClassName
+ " after creating validator object");
try {
logger.log(Level.INFO, myClassName
+ " before performing validation");
lValidator.setErrorHandler(new SimpleErrorHandler());

.log(Level.INFO, myClassName
+ " after performing validation");
catch (SAXException e) {
// TODO Auto-generated catch block
logger.log(Level.INFO, myClassName + " sax exception"
+ e.getMessage());
throw new CAFException(e.getMessage());
} catch (IOException e) {
logger.log(Level.INFO, myClassName + " IOException"
+ e.getMessage());
throw new IOException(e.getMessage());
catch(Exception e)
throw new Exception("xml do not contain xsd,dtd|| xml is not following dtd or xsd");

return true;


i am able to validate when i pass xml and the schema used in the xml.

if xml satisfies schema then i return true.
else iam throwing exception concerned to violation.

It is working fine ,but my problem is suppose i pass xml and the schema that is not used in the xml.

At that situation it is still validating according to schema and i am throwing exception of violation

But i should get output as schema not found.

please give your inputs/ideas to resolve this issue.

Thanks in advance.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!