zoukankan      html  css  js  c++  java
  • xml转换成map

    import java.io.IOException;
    import java.io.StringReader;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.TreeMap;

    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.input.SAXBuilder;
    import org.xml.sax.InputSource;

    public class ParseXml {

    /**
    * xml转换成TreeMap
    * @param xml
    * @return
    */
    public static Map<String, Object> xmlToTreeMap(String xml) {
    Map<String, Object> map = new TreeMap<String, Object>();
    try {
    if (xml == null || "".equals(xml.replaceAll(" ", ""))) {
    return null;
    } else {
    Document doc = (new SAXBuilder()).build(new StringReader(xml));
    Element element = doc.getRootElement();
    if (element == null) {
    throw new Exception("XML包格式错误:没有根元素");
    }
    List it = element.getChildren();
    int len = it.size();
    if (len == 0) {
    throw new Exception("XML包格式错误:没有子元素");
    }
    map = new TreeMap<String, Object>();
    for (int i = 0; i < len; i++) {
    Element subelement = (Element) it.get(i);
    List itc = subelement.getChildren();
    if (itc != null && itc.size() > 0) {
    for (int n = 0; n < itc.size(); n++) {
    Element sub = (Element) itc.get(n);
    map.put(sub.getName().toLowerCase(), sub.getText());
    }
    }
    map.put(subelement.getName().toLowerCase(), subelement.getText());
    }
    }
    } catch (JDOMException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (Exception e) {
    e.printStackTrace();
    }
    return map;
    }

    /**
    * xml转换成HashMap
    *
    * @param xml
    * @return
    */
    public static Map<String, Object> xmlToHashMap(String xml) {
    Map<String, Object> map = new HashMap<String, Object>();
    try {
    if (xml == null || "".equals(xml.replaceAll(" ", ""))) {
    return null;
    } else {
    Document doc = (new SAXBuilder()).build(new StringReader(xml));
    Element element = doc.getRootElement();
    if (element == null) {
    throw new Exception("XML包格式错误:没有根元素");
    }
    List it = element.getChildren();
    int len = it.size();
    if (len == 0) {
    throw new Exception("XML包格式错误:没有子元素");
    }
    map = new HashMap<String, Object>();
    for (int i = 0; i < len; i++) {
    Element subelement = (Element) it.get(i);
    List itc = subelement.getChildren();
    if (itc != null && itc.size() > 0) {
    for (int n = 0; n < itc.size(); n++) {
    Element sub = (Element) itc.get(n);
    map.put(sub.getName().toLowerCase(), sub.getText());
    }
    }
    map.put(subelement.getName().toLowerCase(), subelement.getText());
    }
    }
    } catch (JDOMException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (Exception e) {
    e.printStackTrace();
    }
    return map;
    }

    /**
    * @Title: parseXmlToListMap
    * @Description: xml转listmap
    * @param @param xml
    * @param @return
    * @return List<Map> 返回类型
    * @throws
    */
    @SuppressWarnings("unchecked")
    public static List<Map<String, Object>> parseXmlToListMap(String xml) {
    List<Map<String, Object>> argMapList = new ArrayList<Map<String, Object>>();
    Map<String, Object> retMap = new HashMap<String, Object>();
    try {
    StringReader read = new StringReader(xml);
    // 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
    InputSource source = new InputSource(read);
    // 创建一个新的SAXBuilder
    SAXBuilder sb = new SAXBuilder();
    // 通过输入源构造一个Document
    Document doc = (Document) sb.build(source);
    Element root = doc.getRootElement();// 指向根节点
    if(root != null){
    List<Element> es = root.getChildren();
    if (es != null && es.size() != 0) {
    for (Element element : es) {
    retMap.put(element.getName(), element.getText());
    }
    }
    }
    argMapList.add(retMap);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return argMapList;
    }

    public static void main(String[] args) {
    String xml = "<xml><code><![CDATA[code]]></code><msg><![CDATA[msg]]></msg><responseId>12345678</responseId></xml>";
    System.out.println(parseXmlToListMap(xml).get(0));
    }

    }

  • 相关阅读:
    wpf.xaml.behavior
    为你的硬件自动化统一构建root和firmware
    Dsm as deepin mate(2):在阿里云上真正实现单盘安装运行skynas
    Boot界的”开源os“ : coreboot,及再谈云OS和本地OS统一装机的融合
    0pe单文件夹,grub菜单全外置版
    阿里云上利用virtiope+colinux实现linux系统盘动态无损多分区
    比WEB更自然,jupyter用于通用软件开发的创新意义:使任何传统程序秒变WEB
    编程实践选型通史:*坦无架构APP开发支持与充分batteryincluded的微实践设施
    除了LINUX,我们真的有可选的第二开源操作系统吗?
    聪明的Mac osx本地云:同一生态的云硬件,云装机,云应用,云开发的完美集
  • 原文地址:https://www.cnblogs.com/shihaiming/p/5736883.html
Copyright © 2011-2022 走看看