convert flat file records to xml in java

Arousygully

New Member
I have a scenario where records of employees written in flat file, something like :\[code\] flatFile.txt ============ 1|name1|dept1|10000 2|name2|dept2|12000 3|name3|dept3|9500 .... ....\[/code\]Now I want to read this flat file and convert above employee records into some new xml file everytime, so at the end I should have the xml file with following data :\[code\]<EMPLOYEES> <EMPLOYEE> <ID>1</ID> <NAME>name1</NAME> <DEPARTMENT>dept1</DEPARTMENT> <SALARY>10000</SALARY> </EMPLOYEE> <EMPLOYEE> <ID>2</ID> <NAME>name2</NAME> <DEPARTMENT>dept2</DEPARTMENT> <SALARY>12000</SALARY> </EMPLOYEE> ... ...</EMPLOYEES>\[/code\]Now to implement this concept I need to take care of the validations of the data like :1. id & salary should be numberic2. name length should be less than 203. single line of a flat file should contains above 4 fieldsIf any of the validations fails then they needs to be reflected in the xml file alongwith the error line number, something like :\[code\]<NAME type="Error" Line="2"></NAME> (name length is greater than 20 in 2nd record of a flat file)or<EMPLOYEE type="Error" Line="1"></EMPLOYEE> (first record doesn't contains enough fields)\[/code\]Now applications needs to be designed in such a way that components are pluggable with alternatives. For e.g. It should be possible to replace the parser used to parse an input file on the basis of delimiter with another one which parse it as fixed length.So somehow I will have to design the concept in layered way like Parsing -> Validation -> Output Generation.Now in order to implement this concept I have given a thought, which is as follows :\[code\]a) Read all records from file using BufferedReader and tokenize it using StringTokenizer.b) Initialize the employee object for each record and add them into some collection (List).c) Maintain the errors (mismatch of fields or any other failed validation) into Map<Integer,List<String>>.d) Write or Marshal the list into xml using some XML Builder API (not clear which would be the best).\[/code\]Can anyone give me the better suggestion or any hints to achieve the implementation ?
 
Back
Top