Hi there,
i'm currently trying to write a kind of XML-Parser for my gui framework and i'm struggling with the object creation.
So a possible XML-File could look like this:
<Window id="Notification" title="Notification" message="An Error occured while...">
<button id="Button_try_again" caption="Try again" />
<button id="Button_cancel" caption="cancel" />
</Window>
For iterate through all tags i'm using StaX. The name of each tag is accessible by calling the getLocalName()-method.
Furthermore there is an corresponding class for each valid tag (just like window and button in the example above) and i need to get an object of one of them.
So here is a little snippet of what came to my mind to achieve this:
String tagname = Staxparser.getLocalName();
if(tagname.equalsIgnoreCase("Window")) {
// instantiate window class
} else if(tagname.equalsIgnoreCase("Button")) {
// instantiate button class
}
Well, this looks pretty much nasty and should not be done like this.
So here is the actual question:
Is there a way to not use a whole bunch of if-else-statements to figure out what class needs to be instantiated?
Is there a design pattern or anything else that is able to simplify the problem?
Thanks in advance.