栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java读取xml文件内容,转为ztree的形式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java读取xml文件内容,转为ztree的形式

@ResponseBody
    @RequestMapping("/getXmlNodes")
    public List getXmlNodes(Long versionId){
        try {
            File file = objectFileService.getFileByVersionId(versionId);
            String s;
            InputStreamReader in = new InputStreamReader(new FileInputStream(file), "UTF-8");
            BufferedReader reader = new BufferedReader(in);
            StringBuffer content = new StringBuffer();
            while ((s = reader.readLine()) != null) {
                content = content.append(s + "rn");
            }
            if (null != reader) {
                reader.close();
            }
            JSonObject jsonObject = XMLUtil.xml2JSonObject(content.toString());
            List zTreeNodeList=new ArrayList<>();
            Integer parent = null;
            test(jsonObject,1, parent, zTreeNodeList, null);
            return zTreeNodeList;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return null;
    }

    public void test(Object o,Integer index, Integer parent, List nodes, String xkey) {
        if (o instanceof JSONObject) {
            Iterator it = ((JSONObject) o).keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                if (((JSONObject) o).get(key) instanceof JSonArray
                        || ((JSONObject) o).get(key) instanceof JSONObject) {
                    int nodeId=nodes.size() + 1;
                    if (!(((JSONObject) o).get(key) instanceof JSONArray)) {
                        nodes.add(new ZTreeNode(String.valueOf(nodeId), String.valueOf(parent), key));
                        parent = nodeId;
                    }
                    test(((JSONObject) o).get(key),nodeId, parent, nodes, ((JSONObject) o).get(key) instanceof JSonArray ? key : null);
                }else {
                    Map map = nodes.get(index-1).getAttrMap();
                    if(map==null){
                        map=new HashMap<>();
                    }
                    map.put(key,((JSONObject) o).get(key));
                    nodes.get(index-1).setAttrMap(map);
                }
            }
        } else if (o instanceof JSONArray) {
            JSonArray ja = (JSONArray) o;
            for (Object obj : ja) {
                int nodeId = nodes.size() + 1;
                nodes.add(new ZTreeNode(String.valueOf(nodeId), String.valueOf(parent), xkey));
                test(obj,nodeId, nodeId, nodes, xkey);
            }
        }
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/671793.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号