I am using SAX to parse XML files. Let's suppose that I want my application to only deal with XML files with root element "animalList" - if the root node is something else, the SAX parser should terminate parsing.
Using DOM, you would do it like this:
...
Element rootElement = xmldoc.getDocumentElement();
if ( ! rootElement.getNodeName().equalsIgnoreCase("animalList") )
throw new Exception("File is not an animalList file.");
...
but I can't ascertain how to do it using SAX - I can't figure out how to tell the SAX parser to determine the root element. However, I know how to stop parsing at any point (after seing Tom's solution).
Example XML file:
<?xml version="1.0" encoding="UTF-8"?>
<animalList version="1.0">
<owner>Old Joe</owner>
<dogs>
<germanShephered>Spike</germanShephered>
<australianTerrier>Scooby</australianTerrier>
<beagle>Ginger</beagle>
</dogs>
<cats>
<devonRex>Tom</devonRex>
<maineCoon>Keta</maineCoon>
</cats>
</animalList>
Thanks.