基于jdom 的 xmluti

  1. package cn.com.do1.wechat.common;
  2. import org.jdom.Attribute;
  3. import org.jdom.Document;
  4. import org.jdom.Element;
  5. import org.jdom.JDOMException;
  6. import org.jdom.input.SAXBuilder;
  7. import java.io.IOException;
  8. import java.io.StringReader;
  9. import java.util.*;
  10. /**
  11. * Created by ao.ouyang on 2015/8/18.
  12. */
  13. public class XmlUtil {
  14.    /**
  15.     * 把xml文件转换为map形式,其中key为有值的节点名称,并以其所有的祖先节点为前缀,用
  16.     * "."相连接。如:SubscribeServiceReq.Send_Address.Address_Info.DeviceType
  17.     *
  18.     * @param xmlStr
  19.     *            xml内容
  20.     * @return Map 转换为map返回
  21.     */
  22.    public static Map<String, String> xml2Map(String xmlStr)
  23.            throws JDOMException, IOException {
  24.        Map<String, String> rtnMap = new HashMap<String, String>();
  25.        SAXBuilder builder = new SAXBuilder();
  26.        Document doc = builder.build(new StringReader(xmlStr));
  27.        // 得到根节点
  28.        Element root = doc.getRootElement();
  29.        String rootName = root.getName();
  30.        rtnMap.put("root.name", rootName);
  31.        // 调用递归函数,得到所有最底层元素的名称和值,加入map中
  32.        convert(root, rtnMap, rootName);
  33.        return rtnMap;
  34.    }
  35.    /**
  36.     * 递归函数,找出最下层的节点并加入到map中,由xml2Map方法调用。
  37.     *
  38.     * @param e
  39.     *            xml节点,包括根节点
  40.     * @param map
  41.     *            目标map
  42.     * @param lastname
  43.     *            从根节点到上一级节点名称连接的字串
  44.     */
  45.    public static void convert(Element e, Map<String, String> map,
  46.                               String lastname) {
  47.        if (e.getAttributes().size() > 0) {
  48.            Iterator it_attr = e.getAttributes().iterator();
  49.            while (it_attr.hasNext()) {
  50.                Attribute attribute = (Attribute) it_attr.next();
  51.                String attrname = attribute.getName();
  52.                String attrvalue = e.getAttributeValue(attrname);
  53.                //map.put( attrname, attrvalue);
  54.                map.put(lastname + "." + attrname, attrvalue); //key 根据根节点 进行生成
  55.            }
  56.        }
  57.        List children = e.getChildren();
  58.        Iterator it = children.iterator();
  59.        while (it.hasNext()) {
  60.            Element child = (Element) it.next();
  61.   /*String name = lastname + "." + child.getName();*/
  62.            String name = child.getName();
  63.            // 如果有子节点,则递归调用
  64.            if (child.getChildren().size() > 0) {
  65.                convert(child, map,  lastname + "." + child.getName());
  66.            } else {
  67.                // 如果没有子节点,则把值加入map
  68.                map.put(name, child.getText());
  69.                // 如果该节点有属性,则把所有的属性值也加入map
  70.                if (child.getAttributes().size() > 0) {
  71.                    Iterator attr = child.getAttributes().iterator();
  72.                    while (attr.hasNext()) {
  73.                        Attribute attribute = (Attribute) attr.next();
  74.                        String attrname = attribute.getName();
  75.                        String attrvalue = child.getAttributeValue(attrname);
  76.                        map.put(lastname + "." + child.getName() + "." + attrname, attrvalue);
  77.                        //map.put( attrname, attrvalue);
  78.                    }
  79.                }
  80.            }
  81.        }
  82.    }
  83.    /**
  84.     * 把xml文件转换为list形式,其中每个元素是一个map,map中的key为有值的节点名称,并以其所有的祖先节点为前缀,用
  85.     * "."相连接。如:SubscribeServiceReq.Send_Address.Address_Info.DeviceType
  86.     *
  87.     * @param xmlStr
  88.     *            xml内容
  89.     * @return Map 转换为map返回
  90.     */
  91.    public static List<Map<String, String>> xml2List(String xmlStr)
  92.            throws JDOMException, IOException {
  93.        List<Map<String, String>> rtnList = new ArrayList<Map<String, String>>();
  94.        Map<String, String> rtnMap = new HashMap<String, String>();
  95.        SAXBuilder builder = new SAXBuilder();
  96.        Document doc = builder.build(new StringReader(xmlStr));
  97.        // 得到根节点
  98.        Element root = doc.getRootElement();
  99.        String rootName = root.getName();
  100.        rtnMap.put("root.name", rootName);
  101.        // 调用递归函数,得到所有最底层元素的名称和值,加入map中
  102.        convert2List(root, rtnMap, rootName, rtnList);
  103.        if (rtnList.size() == 0)
  104.            rtnList.add(rtnMap);
  105.        return rtnList;
  106.    }
  107.    /**
  108.     * 递归函数,找出最下层的节点并加入到map中,如果有相同的节点,则加入list中, 由xml2List方法调用。
  109.     *
  110.     * @param e
  111.     *            xml节点,包括根节点
  112.     * @param map
  113.     *            目标map
  114.     * @param lastname
  115.     *            从根节点到上一级节点名称连接的字串
  116.     * @param list
  117.     *            相同节点生成map放入list中
  118.     */
  119.    public static void convert2List(Element e, Map<String, String> map,
  120.                                    String lastname, List<Map<String, String>> list) {
  121.        if (e.getAttributes().size() > 0) {
  122.            Iterator it_attr = e.getAttributes().iterator();
  123.            while (it_attr.hasNext()) {
  124.                Attribute attribute = (Attribute) it_attr.next();
  125.                String attrname = attribute.getName();
  126.                String attrvalue = e.getAttributeValue(attrname);
  127.                map.put(attrname, attrvalue);
  128.            }
  129.        }
  130.        List children = e.getChildren();
  131.        Iterator it = children.iterator();
  132.        while (it.hasNext()) {
  133.            Element child = (Element) it.next();
  134.            String name = lastname + "." + child.getName();
  135.            // 如果有子节点,则递归调用
  136.            if (child.getChildren().size() > 0) {
  137.                convert(child, map, name);
  138.            } else {
  139.                // 如果没有子节点,则把值加入map
  140.                map.put(name, child.getText());
  141.                // 如果该节点有属性,则把所有的属性值也加入map
  142.                if (child.getAttributes().size() > 0) {
  143.                    Iterator attr = child.getAttributes().iterator();
  144.                    while (attr.hasNext()) {
  145.                        Attribute attribute = (Attribute) attr.next();
  146.                        String attrname = attribute.getName();
  147.                        String attrvalue = child.getAttributeValue(attrname);
  148.                        map.put(name + "." + attrname, attrvalue);
  149.                    }
  150.                }
  151.            }
  152.            // 如果有相同节点,则加入list中,不考虑子节点中又有相同节点的情况
  153.            if (e.getChildren(child.getName()).size() > 1) {
  154.                Map<String, String> aMap = new HashMap<String, String>();
  155.                aMap.putAll(map);
  156.                list.add(aMap);
  157.                map = new HashMap<String, String>();
  158.                map.put("root.name", aMap.get("root.name"));
  159.            }
  160.        }
  161.    }
  162. }


posted on 2017-03-22 08:50  signheart  阅读(306)  评论(0编辑  收藏  举报

导航