Skip to content

Instantly share code, notes, and snippets.

@acmi
Created August 11, 2014 13:04
Show Gist options
  • Save acmi/e2ebae96dd3cf9d883b6 to your computer and use it in GitHub Desktop.
Save acmi/e2ebae96dd3cf9d883b6 to your computer and use it in GitHub Desktop.
JAXBValidator
import classes.Products;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.util.JAXBSource;
import javax.xml.transform.Source;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.File;
public class JAXBValidator {
public static void main(String[] args) throws Exception{
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("products.xsd"));
JAXBContext context = JAXBContext.newInstance(Products.class);
JAXBSource source = new JAXBSource(context, null);
Validator validator = schema.newValidator();
validator.validate(new Source() {
@Override
public void setSystemId(String systemId) {
}
@Override
public String getSystemId() {
return null;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment