简体   繁体   English

Java类,用于通过内部类实现SAX Parser

[英]Java class for implementing a SAX Parser with inner classes

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. 对于大学任务,我需要编写一个SAX解析器和一个过滤器,该过滤器读取原始XML文件,然后创建一个新的修改后的文件。 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. 分配要求我的程序需要使用“ java Sax inputFileDestination OutputFileDestination”通过控制台运行,并且只需要一个文件。 I means I need to implement the interfaces with in the Sax.java. 我的意思是我需要在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. 我对内部类很熟悉,但是我不知道如何在外部类中使用main方法来实现这一点。

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. 您的类将有一个main方法,该方法创建一个SAXParser,并将其自身注册为回调。 You will then implement the SAX methods you desire (startElement, endElement, characters) from HandlerBase. 然后,您将从HandlerBase实现所需的SAX方法(startElement,endElement,字符)。

All your're doing is writing a SAXParser, and then tacking a main() method on it, so it will run from the command line. 您要做的只是编写一个SAXParser,然后在其上添加一个main()方法,因此它将从命令行运行。

Consider creating a MySAXParser class with a usage outside in another class having a main . 考虑创建一个MySAXParser类,该类在另一个具有main类中具有外部用法。 Then place that main method inside your MySAXParser class as test code. 然后将该main方法放入MySAXParser类中作为测试代码。

XML Parsers deliver a jar with a META-INF/MANIFEST.MF using the SPI (Service Provider Interface). XML解析器使用SPI(服务提供商接口)来提供带有META-INF / MANIFEST.MF的jar。 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 ; 如果其他内部类是独立的,则它们可以是private static ,也可以只是private存储额外的外部MySAXParser.this so one error often made is forgetting static . 所以经常犯的一个错误是忘记static

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM