zoukankan      html  css  js  c++  java
  • xml转为Map集合以及对象转化为xml

    package com.fang.util;
     
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
     
    import javax.servlet.http.HttpServletRequest;
     
    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
     
    import com.fang.entity.TextMessage;
    import com.thoughtworks.xstream.XStream;
     
    /**
     * xml转为Map集合
     * @author Administrator
     *
     */
    public class MessageUtil {
        public static Map<String, String> xmlToMap(HttpServletRequest request){
            Map<String, String> map = new HashMap<String, String>();
            SAXReader reader = new SAXReader();
            try {
                InputStream ins = request.getInputStream();
                 
                Document doc = reader.read(ins);
                Element root = doc.getRootElement();
                 
                @SuppressWarnings("unchecked")
                List<Element> list = root.elements();
                for (Element e : list) {
                    map.put(e.getName(), e.getText());
                }
                ins.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return map;
        }
         
        /**
         * 将对象转化为xml
         * @param testMessage
         * @return
         */
        public static String textMessageToXml(TextMessage testMessage){
            XStream xstream = new XStream();
            xstream.alias("xml",testMessage.getClass());
            return xstream.toXML(testMessage);
        }
         
         
         
    }
  • 相关阅读:
    分页 存储过程
    Base64编码
    汉字转拼音 完整类
    C#利用SharpZipLib解压或压缩文件夹实例操作
    C#压缩解压zip 文件
    MapReduce shuffle原理
    设计模式(一)—— 代理模式
    Lombok的基本使用
    解决idea 每次新建项目需要重新配置maven
    10-20 Spring框架(三)—— AOP核心
  • 原文地址:https://www.cnblogs.com/jasonlu1016/p/5226705.html
Copyright © 2011-2022 走看看