Traditionally, XML APIs are either: Both have advantages: DOM, for example, allows for random access to the document, and event driven algorithm like SAX has a small memory footprint and is typically much faster.
These two access metaphors can be thought of as polar opposites.
Ok, here is the source code (minus comments, error handling and class declaration of the actual sample class -- for full class, see the link above), with comments indicating where each step starts: // step 1: get schema factory XMLValidation Schema Factory sf = XMLValidation Schema Instance(XMLValidation Schema.
SCHEMA_ID_RELAXNG); // step 2: construct validation schema instance XMLValidation Schema rng = sf.create Schema(new File(args)); // step 3: construct stream reader XMLInput Factory2 ifact = (XMLInput Factory2)XMLInput Instance(); XMLStream Reader2 sr = ifact.create XMLStream Reader(new File(args)); // step 4: enable validation sr.validate Against(rng); // step 5: stream through the document: while (Next()) // done!
And it does all of this efficiently, that is, fast.