Validating xml with xsd java
W3C_XML_SCHEMA_NS_URI); URL schema URL = // The URL to your XML Schema; Schema schema = Schema(schema URL); Schema(schema); Customer customer = (Customer) unmarshaller.unmarshal(xml); SAXParser Factory factory = SAXParser Instance(); Validating(true); Namespace Aware(true); SAXParser parser = SAXParser(); Property(" Language", " XMLReader reader = XMLReader(); Error Handler(new Error Handler()); reader.parse(new Input Source("document.xml")); JAXB is the Java standard XML parsing library: Now I'm trying to validate with another schema that uses import and one include. I have used in my Java 8 the following code which allows to specify an embedded schema path other than the root path: import org.apache.xerces. But versions 4.3 and above use the two additional schema files. I have to assume that your other two XSDs are also packed as resources in the same jar, in the same directory.I have to admit that I am intrigued by the error message, which seems to imply the schema it loaded is valid, so it might be an XML issue; if the above doesn't help you, then you should consider posting the offending XML as well...UPDATE: As per my comments, it is working as described.See code snippet below: Schema Factory schema Factory = Schema Instance(" Schema compiled Schema = schema Schema(new SOTests()Class() Class Loader()Resource("general Invoice Request_430.xsd")); Validator validator = compiled Validator(); try catch (SAXException ex) You don't need to load more than the first XSD, and you shouldn't, since all the imports provide hints to the schema location.
Although a full treatment of XML Schema is beyond the scope of this tutorial, this section shows you the steps you take to validate an XML document using an XML Schema definition.
To be notified of validation errors in an XML document, the following must be true: It is helpful to start by defining the constants you will use when configuring the factory.
These are the same constants you define when using XML Schema for SAX parsing, and they are declared at the beginning of the Because JAXP-compliant parsers are not namespace-aware by default, it is necessary to set the property for schema validation to work.
*/ public class Resource Resolver implements LSResource Resolver The accepted answer is perfectly ok, but does not work with Java 8 without some modifications. W3C_XML_SCHEMA_NS_URI); Schema schema = schema Schema(new File("../foo.xsd")); Validator validator = Validator(); validator.validate(new Stream Source(new File("./foo.xml"))); Schema Factory schema Factory = Schema Factory Instance(XMLConstants.
LSResource Resolver; /** * The Class Resource Resolver. Objects; public class Resource Resolver implements LSResource Resolver Schema Factory schema Factory = Schema Instance(XMLConstants.This code works for me know, but please keep in mind that it assumes all xsd files have a unique name. If you have some xsd files with same name, but different content at different paths, it will probably give you problems. This might depends on the library you use but googling around with "how to validate xml file in java" gave me these results where you might find your answer: first interesting result second interesting result Validator v = Language(Languages.