Hi everyone, I am doing a project that I need to parse may different xml config files, such as struts config and tiles config and some other with different structures. My question is that if I use SAX, do I need to create parser for each file or I just create one for all? If there is only one parser, I worry about the if statement will go too large and mess. Is there a better way if I only use SAX ?
William Brogden wrote:I think that trying to re-use a SAX parser would be a mistake. But note that you can re-use the same instance of SAXParserFactory.
Perhaps you meant re-use the same handler for the SAX events? That would indeed lead to a mess.
If this was my problem I would look into the Apache Commons Digester project.
Thanks for your reply.
Yes, what I consider is to re-use the same handler for SAX events. I think there would be a mess too. I am still looking for the better tool than SAX.
The reason I use SAX is because my app should handle different structure of xml. So I should create a lot of objects for one xml file if I use Digester. If a lot of xml files, I create a lot of objects, thats heavy. For SAX, I just add a lot of if statement in the startElement() method for searching for a tag or attributes.
Seems to me the important question is - how many values do you need to keep out of each configuration file?
If you only need one or two, sure, the SAX parser (or perhaps StAX because it is easier to stop parsing once the value is found.)
On the other hand if you need many values extracted, it is purely false economy to create hard to debug and maintain monster SAX handler class(es) - this counts as premature optimization. (The root of all evil as all programmers should remember.) In that case, just build the darn objects and go on to more important architectural tasks. Or - just build the DOMs, apply XPATH to get the values and discard the DOM - slow but oh so easy to maintain.
So - how much of the data in a typical conf file do you need? When do you need it? Do you have to re-read it often?
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop