I am parsing a custom XML configuration file in a Java application. I am trying to use the SAX parser, mainly because I need to report errors in the configuration with line numbers.
There are a lot of code samples online of implementing a handler class, and things seem fairly straightforward for normal processing - for example, http://tutorials.jenkov.com/java-xml/sax-example.html
But in my case, sometimes I need to skip an entire tree under an element:
<sampledocument>
<sampletag>
<process/>
<these/>
<tags/>
</sampletag>
<sampletag skip="yes">
<do_not>
<process/>
<these/>
<tags/>
</sampletag>
<sampledocument>
LATER ADDITION: Moreover, I only know whether to skip at runtime. In a somewhat contrived example, I would need to open a file to process the tags under <sampletag>, and if the file is not found, not process them:
<sampledocument>
<sampletag file="file1">
<process/>
<these/>
<tags/>
<if_file1_exists/>
</sampletag>
<sampletag file="file2">
<process/>
<these/>
<tags/>
<if_file2_exists/>
</sampletag>
<sampledocument>
Of course, I can just track skipping in the handler code, but this is a bit awkward. Can I somehow tell SAX in the startElement() method to just skip the contents of this element?