1) Create a Java class, that maps to the data in the flat file, for ex:
public class Item {
private String itemId;
private String description;
/**
* @return the itemId
*/
public String getItemId() {
return itemId;
}
/**
* @param itemId the itemId to set
*/
public void setItemId(String itemId) {
this.itemId = itemId;
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
}
2) Parse the flat file into a list of 'Items' (List of Item objects)
3) Use a good, lightweight framework like 'xStream' and use the appropriate method to serialize Java object to XML file. For example: xStream.toXml(Object obj, Writer out)
PS: This is just a standard way (using well tested frameworks and hence, not re-inventing the wheel), but not the optimal one. Optimally, for performance and less memory footprint, you can parse the flat file and write to a XML file at the same time.