I have two xsd files to validate a xml. But the problem is my code takes only one xsd. How to use other xsd in the following code? I dont have idea about where should i place/call 2nd xsd file.
private void validate(File xmlF,File xsd1,File xsd2) {
try {
url = new URL(xsd.toURI().toString());// xsd1
} catch (MalformedURLException e) {
e.printStackTrace();
}
source = new StreamSource(xml); // xml
try {
System.out.println(url);
schema = schemaFactory.newSchema(url);
} catch (SAXException e) {
e.printStackTrace();
}
validator = schema.newValidator();
System.out.println(xml);
try {
validator.validate(source);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Plenty of hits when searching on SO or Google. One of them is this question, where the author found his own solution and reports the following code to add multiple xsd’s to the validator:
However, when working directly with
InputStreamonStreamSource, the resolver is not able to load any referenced XSD files. If, for instance, the filexsd1imports or includes a third file (which is notxsd2), schema creation will fail. You should either set the system identifier (setSystemId) or (even better) use theStreamSource(File f)constructor.Adjusted to your example code:
Note:
If working with classpath resources, I’d prefer the
StreamSource(String systemId)constructor (rather than creating aFile):