Some folks have some rather strange superstitions when it comes to programming. Take for example Maik Schreiber's colleague. He believes that directly comparing a string variable (xmlTagName) to a string literal (“sub_name“) will make your program run as slow as cold molasses. So, to avoid this, he built a data structure with all of the possible names and, for every xml tag in the document, searched through this data structure to find the tag name, and did the appropriate action ...

private static final int NEWSLETTER_NAME = 0;
private static final int NEWSLETTER_SUB_NAME = 1;
private static final int NEWSLETTER_SCREEN_NAME = 2;
private static final int NEWSLETTER_EXTERNAL_NAME = 3;
private static final int NEWSLETTER_ID = 4;
//[ED: some more fields skipped]

private static final Map XMLNEWSLETTERELEMENTS = new HashMap();
static {
        XMLNEWSLETTERELEMENTS.put("name", new Integer(0));
        XMLNEWSLETTERELEMENTS.put("sub_name", new Integer(1));
        XMLNEWSLETTERELEMENTS.put("screen_name", new Integer(2));
        XMLNEWSLETTERELEMENTS.put("external_name", new Integer(3));
        XMLNEWSLETTERELEMENTS.put("id", new Integer(4));
        //[ED: rest skipped]
}

//[ED: and then, in a for loop that handles all XML Element tag types]
switch (((Integer) XMLNEWSLETTERELEMENTS.get(cElement.getName)).intValue()) {
        case NEWSLETTER_NAME: {
                cmNewsObject.setName(cElement.getTextTrim());
                break;
        }
        case NEWSLETTER_SUB_NAME: {
                cmNewsObject.setSubName(cElement.getTextTrim());
                break;
        }
        case NEWSLETTER_SCREEN_NAME: {
                cmNewsObject.setScreenName(cElement.getTextTrim());
                break;
        }
        case NEWSLETTER_EXTERNAL_NAME: {
                cmNewsObject.setExternalName(cElement.getTextTrim());
                break;
        }
        case NEWSLETTER_ID: {
                cmNewsObject.setId(Long.valueOf(cElement.getTextTrim());
                break;
        }
        //[ED: you get the idea]
}

 

 

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!