I think the flags are not very scalable if you have a lot of different questions to parse, and neither are the global variables to hold the results... if you have 100 questions then you'll need 100 variables, and when they change over time it will be a bear to keep them up to date. I would use a map structure to hold the result, and another one to hold the correspondence between each question text and the corresponding field you are trying to capture (this is not actual Java, just an approximation):
public Map parseDemographicInformation(XmlStream xml, Map questionMap) { Map record = new Map(); String field = "id"; while((elem = xml.getNextElement())) { if(elem.tagName == "question") { field = questionMap[elem.value]; } else if(elem.tagName == "value") { record[field] = elem.value; } } return record; }
Then you have something like this to output the result:
String[] fieldsToOutput = { "id", "firstName", "lastName" }; // ideally read this from a file too so it can be changed dynamically // ... for(int i=0; i < fieldsToOutput.length; i++){ if(i > 0) System.out.print("~"); System.out.print(record[fieldsToOutput[i]]); } System.out.println();