aspose file tools*
The moose likes XML and Related Technologies and the fly likes XML file validation with the XSD 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 "XML file validation with the XSD" Watch "XML file validation with the XSD" New topic
Author

XML file validation with the XSD

ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
Hi all,

I am using the following code for validation of a xml file with its corresponding XML schema. The validation is successful even when there is a non-compliance with the schema. For eg., for an element which is declared as
minOccurs and maxOccurs to be "1" (mandatory attribute), and removing this element in the XML document, doesnt show that the file is invalid. But any incorrect semantics are being found through this code (like missing "<"http://apache.org/xml/features/validation/schema", true);
parser.setProperty(
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
"Latest_DWS.xsd");
parser.parse("promotionPlanChangeMA_ALL.xml");
System.out.print("Successful");
}
catch (Exception e)
{
System.out.print("Problem parsing the file.");
e.printStackTrace();
}
}
}

Many thanks in advance!!
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Hi ram,
Kindly use code tags while posting code.
You have not specified which parser are you using. Every parser has different ways of doing validations and getting the validation results. It is not necessary that if an xml does not adhere to the specified xsd, an exception will be thrown while parsing. For example, xml beans does not validate the xml while parsing but it requires an explicit call to a validate method.


apigee, a better way to API!
ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
Hi,

Sorry the code was not completely copied. I am using DOM parser.

Here is the code I used -

==========================================================

public static void main (String args[])
{

try
{
DOMParser parser = new DOMParser();
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
parser.setProperty(
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
"Latest_DWS.xsd");
//ErrorChecker errors = new ErrorChecker();
//parser.setErrorHandler(errors);
parser.parse("promotionPlanChangeMA_ALL.xml");
System.out.print("Successful");
}
catch (Exception e)
{
System.out.print("Problem parsing the file.");
e.printStackTrace();
}
}

==========================================================

Please let me know how to proceed.

Thanks in advance!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XML file validation with the XSD
 
Similar Threads
if no xsd is mentioned in xml possible to assign default xsd
Validate against schema
validate schema in DOM
javax.xml.validation doubt!
XML file validation with the XSD