This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
.newInstance("http://www.w3.org/2001/XMLSchema";);

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();
logger
.log(Level.INFO, myClassName
+ " after creating validator object");
try {
logger.log(Level.INFO, myClassName
+ " before performing validation");
lValidator.validate(lSource);
lValidator.setErrorHandler(new SimpleErrorHandler());

logger
.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.
 
He's my best friend. Not yours. Mine. You can have this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic