For a college assignment I need to write a SAX parser and a filter that reads the original XML file then creates a new modified one. The assignment requires that my program need to be run by console with "java Sax inputFileDestination OutputFileDestination" and it requires that there is only one file. I means I need to implement the interfaces with in the Sax.java. I am familiar with the inner classes but I dont know how to implement this with a main method in the outer class.
Any sugestions?
Since this is an assignment, I'm not going to post any code, but explain how to do it.
I don't think you need an inner class at all to do this. Your class will have a main method, which creates a SAXParser, and registers itself (this) as a callback. You will then implement the SAX methods you desire (startElement, endElement, characters) from HandlerBase.
All your're doing is writing a SAXParser, and then tacking a main() method on it, so it will run from the command line.
Consider creating a MySAXParser class with a usage outside in another class having a main
. Then place that main
method inside your MySAXParser class as test code.
XML Parsers deliver a jar with a META-INF/MANIFEST.MF using the SPI (Service Provider Interface). You might think of doing the same thing as a bonus.
Other inner classes can be private static
if they are independent, or just private
storing an extra outer MySAXParser.this
; so one error often made is forgetting static
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.