zoukankan      html  css  js  c++  java
  • 解析XML

    方式一:

    方式二:

    方式三:

    方式N:

    好可爱的demo:

    document.xml文件内容如下:
    <?xml version="1.0" encoding="UTF-8"?>
        <node>
            <key1> value1</ key1>
            < key2> value 2</ key2>
            < key3> value 3</ key3>
        </node>
    
    
    
    public class ParseXml {
        public static Map<String, String> parseFile(String filePath) {
            File file = new File(filePath);
            if (!file.exists()) {
                return null;
            }
            try {
                Map<String, String> map = new HashMap<String, String>();
                //解析方式不限 
                SAXReader reader = new SAXReader();
                Document document = reader.read(file);
                Node node = document.selectSingleNode("node/key1");
                map.put("key1", node.getText());
                node = document.selectSingleNode("node/key2");
                map.put("key2", node.getText());
                node = document.selectSingleNode("node/key3");
                map.put("key", node.getText());
                return map;
            } catch (Exception e) {
                System.err.println("解析文件异常");
                e.printStackTrace();
                return null;
            }
        }
    
        public static void main(String[] args) {
            Map<String, String> resultMap = parseFile("D:/document.xml");
        }
    }
  • 相关阅读:
    字符串编码js第三方类库text-encoding
    SQL SERVER数据库权限分配
    天地图显示不全
    运用shapefile.js解析Shp文件
    基于Nginx搭建RTMP/HLS视频直播服务器
    centos pptp客户端 连接服务端
    zabbix如何配置微信报警
    zabbix使用web界面设置邮件报警
    linux系统如何查看某一进程的启动时间
    cobbler自动化安装Linux系统
  • 原文地址:https://www.cnblogs.com/alipayhutu/p/2498678.html
Copyright © 2011-2022 走看看