This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes reg xml validation when i pass xml and the schema that is not used in xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "reg xml validation when i pass xml and the schema that is not used in xml" Watch "reg xml validation when i pass xml and the schema that is not used in xml" New topic
Author

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

anish reddy
Ranch Hand

Joined: Apr 19, 2008
Posts: 74
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reg xml validation when i pass xml and the schema that is not used in xml
 
Similar Threads
multiple validation errors in xml for given xsd with java
from jaxp program how to get schema/dtd name present in xml
all validation error using saxParser
XML schema validation program error
valiating xml using multiple schemas