`
mjs123
  • 浏览: 34114 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
文章分类
社区版块
存档分类
最新评论

把map 解析成xml 并通过struts 2 框架转化发送IO流

阅读更多

import java.util.List;
import java.util.Map;

import com.siku.manorage.common.exception.ManorageException;


/**
 * 返回给客户端的信息
 *
 * @author user
 *
 */
public class SendClientXml

 /**
  * 把一个map 对象转化为一个string 类型的xml 文件
  * @param o 传过来一个map 对象
  * @return   返回一个string 类型的xml 文件
  */
 @SuppressWarnings("unchecked")
 public String sendClientXml(Map<String,Object> o)
 {
  StringBuilder xml =new StringBuilder();
  xml.append( "<command>");
  for(String s:o.keySet())
  {
   Object a = o.get(s);
   if(null == a)
   {
    a = new String("");
   }
   Class c = a.getClass();
   Class cc [] =c.getInterfaces();
   boolean b = false;
   for(int i =0; i< cc.length;i++){
    if(cc[i].toString().contains("java.util.List")){
     b = true;
        break;
    }
   }
   if(b==true)
   {  
    List li = (List)a;
    
    for(int i =0; i < li.size(); i++)
    {
     xml.append("<item>");
        Map<String,Object> map = (Map<String,Object>)li.get(i);
        for( Map.Entry<String,Object> n: map.entrySet()){
         xml.append("<"+n.getKey()+">" +n.getValue() +"</"+n.getKey()+">");
        }
        xml.append("</item>");
    }
    
   }
   else{
    xml.append("<"+s+">" +a +"</"+s+">");
   }
  }
  xml.append("</command>");
  return xml.toString();
}
 /**
  * 根据返回的异常信息得到xml 文件返回给前台
  * @param e   ManorageException 异常
  * @return string
  */
 public String sendclients (ManorageException e){
  String xml = "<command>";
  xml += "<s>"+e.getErrorNum()+"</s>";
  xml += "<msgType>" +e.getCommandId()+"</msgType>";
  xml += "<key>" +e.getKey()+"</key>";
  xml += "</command>";
  
  return xml;
 }
 
 public String sendclientx(ManorageException e){
  String xml = "<command>";
  xml += "<s>"+e.getErrorNum()+"</s>";
  xml += "<msgType>" +e.getCommandId()+"</msgType>";
  xml += "<key>" +e.getKey()+"</key>";
  xml += "<a>"+e.getDeductMoney()+"</a>";
  xml += "</command>";
  
  return xml;
 }
 
 
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics