Skip to content

Instantly share code, notes, and snippets.

@zubairov
Created April 8, 2011 12:01
Show Gist options
  • Save zubairov/909706 to your computer and use it in GitHub Desktop.
Save zubairov/909706 to your computer and use it in GitHub Desktop.
Lazy schema loading using Xerces API
private static void validate(String xml) {
String validationFeature = "http://xml.org/sax/features/validation";
String schemaFeature = "http://apache.org/xml/features/validation/schema";
try {
XML11Configuration config = new XML11Configuration();
config.setEntityResolver(new MyEntityManager());
XMLReader r = new SAXParser(config);
r.setFeature(validationFeature, true);
r.setFeature(schemaFeature, true);
r.parse(new InputSource(new StringReader(xml)));
} catch (SAXException e) {
System.out.println(e.toString());
} catch (IOException e) {
System.out.println(e.toString());
}
}
private static class MyEntityManager extends XMLEntityManager {
@Override
public XMLInputSource resolveEntity(
XMLResourceIdentifier resourceIdentifier) throws IOException,
XNIException {
XMLInputSource result = super.resolveEntity(resourceIdentifier);
if (result.getPublicId() == null && result.getSystemId() == null
&& result.getCharacterStream() == null
&& result.getByteStream() == null) {
System.out.println("Can't resolve namespace: "
+ resourceIdentifier.getNamespace());
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment