Builds XML using recursive methods for list having map,table or list data
import java.util.ArrayList; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; public class XMLBuilder { /** * Public Method to return the response in requested format * @param context * @param paramList * @param sFormatType * @return * @throws Exception */ public Object getFormattedResponse(HashMap paramMap) throws Exception { if(paramMap != null) { String sFormat = (String) paramMap.get("format"); System.out.println("getFormattedResponse:: Format from Request::"+sFormat); if(sFormat!= null && sFormat.equalsIgnoreCase("XML")) { Object retObject = paramMap.get("returnObject"); return getXMLResponse(retObject); } } else { System.out.println("getFormattedResponse:: Returning NULL as ParamMap is empty"); } return null; } /** * Method to return XML format * @param context * @param paramList * @return XML String */ private Object getXMLResponse( Object paramObj) { if (paramObj != null && paramObj.getClass().equals(ArrayList.class)) { ArrayList paramList = (ArrayList) paramObj; StringBuffer sb = new StringBuffer(); sb.append("<mwsList type="TopList">"); for (Object object : paramList) { if(object == null) { System.out.println("Object is null .."); }else if (object.getClass().equals(HashMap.class)) { sb.append(getMapdata(object)); } else if (object.getClass().equals(Hashtable.class)) { sb.append(getTabledata(object)); }else if (object.getClass().equals(ArrayList.class)) { sb.append(getArrayListdata(object)); } else if(object.getClass().equals(String.class)) { sb.append("<mwsItem><mwsField><mwsKey></mwsKey><mwsVal>"); sb.append(object); sb.append("</mwsVal></mwsField></mwsItem>"); } else { System.out.println("getXMLResponse:: Data Structure Class Not Handled -- "+object.getClass().getName()); } } sb.append("</mwsList>"); System.out.println("getXMLResponse :: Returning String = "+sb.toString()); return sb.toString(); } else { System.out.println("getXMLResponse :: Returning Object = "+paramObj); return paramObj; } } /** * Method to get data from List */ private Object getArrayListdata(Object object) { List lst = (List) object; Iterator it = lst.iterator(); StringBuffer sb = new StringBuffer(); int index =0; sb.append("<mwsList>"); while(it.hasNext()) { sb.append("<mwsItem>"); System.out.println("getArrayListdata:: Processing - "+it.next()); Object val = lst.get(index); sb.append("<mwsField>"); sb.append("<mwsKey>"); sb.append(""); sb.append("</mwsKey>"); sb.append("<mwsVal>"); if(val != null && val.getClass().equals(String.class)) { sb.append(val); } else if(val != null && val.getClass().equals(HashMap.class)) { sb.append(getMapdata(val)); } else if(val != null && val.getClass().equals(ArrayList.class)) { sb.append(getArrayListdata(val)); }else if(val != null && val.getClass().equals(Hashtable.class)) { sb.append(getTabledata(val)); } sb.append("</mwsVal>"); sb.append("</mwsField>"); index++; sb.append("</mwsItem>"); } sb.append("</mwsList>"); return sb.toString(); } /** * Method to check for Map class and get the data from it */ private String getMapdata(Object object) { Map map = (Map) object; Set set = map.keySet(); Iterator it = set.iterator(); StringBuffer sb = new StringBuffer(); String sKey = ""; String sVal = ""; sb.append("<mwsItem>"); while(it.hasNext()) { sKey = (String) it.next(); Object val = map.get(sKey); sb.append("<mwsField>"); sb.append("<mwsKey>"); sb.append(sKey); sb.append("</mwsKey>"); sb.append("<mwsVal>"); if(val != null && val.getClass().equals(String.class)) { sb.append(val); } else if(val != null && val.getClass().equals(HashMap.class)) { sb.append(getMapdata(val)); } else if(val != null && val.getClass().equals(ArrayList.class)) { sb.append(getArrayListdata(val)); }else if(val != null && val.getClass().equals(Hashtable.class)) { sb.append(getTabledata(val)); } sb.append("</mwsVal>"); sb.append("</mwsField>"); } sb.append("</mwsItem>"); return sb.toString(); } /** * Method to get the data from HashTable */ private String getTabledata(Object object) { Hashtable map = (Hashtable) object; Set set = map.keySet(); Iterator it = set.iterator(); StringBuffer sb = new StringBuffer(); String sKey = ""; String sVal = ""; sb.append("<mwsItem>"); while(it.hasNext()) { sKey = (String) it.next(); Object val = map.get(sKey); sb.append("<mwsField>"); sb.append("<mwsKey>"); sb.append(sKey); sb.append("</mwsKey>"); sb.append("<mwsVal>"); if(val != null && val.getClass().equals(String.class)) { sb.append(val); } else if(val != null && val.getClass().equals(HashMap.class)) { sb.append(getMapdata(val)); } else if(val != null && val.getClass().equals(ArrayList.class)) { sb.append(getArrayListdata(val)); }else if(val != null && val.getClass().equals(Hashtable.class)) { sb.append(getTabledata(val)); } sb.append("</mwsVal>"); sb.append("</mwsField>"); } sb.append("</mwsItem>"); return sb.toString(); } }