Created
April 8, 2011 12:01
-
-
Save zubairov/909706 to your computer and use it in GitHub Desktop.
Lazy schema loading using Xerces API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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