zoukankan      html  css  js  c++  java
  • Dom4j把xml转换成Map(固定格式)

    /**

    * 可解析list

    * @param fileName

    * @return

    * @throws Exception

    */

    @SuppressWarnings("unchecked")

    public Map<String, Object> parserXmlToJSONObjectByArray(String fileName) throws Exception {

    // File inputXml = new File(fileName);

    InputStream iStream = new ByteArrayInputStream(fileName.getBytes());

    SAXReader saxReader = new SAXReader();

    Map<String, Object> returnMap = new HashMap<String, Object>();

    Reader r = new InputStreamReader(iStream, "UTF-8");

    Document document = saxReader.read(r);

    Element employees = document.getRootElement();

    Map<String, Object> jsontotal = new HashMap<String, Object>();

    for (Iterator<?> i = employees.elementIterator(); i.hasNext();) {

    Element employee = (Element) i.next();

    List<Map<String, Object>> jarry = new ArrayList<Map<String, Object>>();

    for (Iterator<?> j = employee.elementIterator(); j.hasNext();) {

    Element node = (Element) j.next();

    Map<String, Object> jsondetail = new HashMap<String, Object>();

    for (Iterator<Element> k = node.elementIterator(); k.hasNext();) {

    Element node_child = (Element) k.next();

    jsondetail.put(node_child.getName(), node_child.getText());

    }

    jarry.add(jsondetail);

    }

    jsontotal.put(employee.getName(), employee.getText());

    returnMap.put("jsontotal", jsontotal);

    if (jarry != null && jarry.size() > 0) {

    returnMap.put("jsonarray", jarry);

    }

    }

    return returnMap;

    }

    Dom4j把xml转换成Map(非固定格式)

     

    将xml转换成Map,能够应对不用结构的xml,而不是只针对固定格式的xml.
    转换规则:
    1.主要是Map与List的互相嵌套
    2.同名称的节点会被装进List

  • 相关阅读:
    04 类与对象
    03 方法
    02-JAVA语法基础
    大道至简第二章 读后感
    leetcode 65 有效数字
    leetcode 670 最大交换
    leetcode 49 字母异位词分组
    leetcode 71 简化路径
    leetcode 2 两数相加
    java compareTo 与 equals 区别
  • 原文地址:https://www.cnblogs.com/zhaoyan001/p/6043503.html
Copyright © 2011-2022 走看看