Question

I need to validate XML against XSD and so what are free XML Validator available out there that could be useful for my task.

Thanks.

Was it helpful?

Solution

xerces has Java, C++, and Perl versions.

The perl version contains a command line validator for convenience.

The Java version API includes classes and example code for validation

Example code:

// parse an XML document into a DOM tree
DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance();
parserFactory.setNamespaceAware(true);
DocumentBuilder parser = parserFactory.newDocumentBuilder();
Document document = parser.parse(new File("instance.xml"));

// create a SchemaFactory capable of understanding WXS schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

// load a WXS schema, represented by a Schema instance
Source schemaFile = new StreamSource(new File("mySchema.xsd"));
Schema schema = factory.newSchema(schemaFile);

// create a Validator instance, which can be used to validate an instance document
Validator validator = schema.newValidator();

// validate the DOM tree
try {
    validator.validate(new DOMSource(document));
} catch (SAXException e) {
    // instance document is invalid!
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top