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");
        }
    }
  • 相关阅读:
    一些程序员可以了解的项目/内容
    为人处世的细节(转自知乎):不炫富, 不哭穷
    centos 6.5 搭建ftp 服务器(vsftpd的配置文件说明)
    linux安装脚本
    Sublime Text3 + Golang搭建开发环境
    go语言环境搭建+sublime text3(windows环境下)
    Enterprise Solution 企业管理软件开发框架
    DotnetSpider爬虫采集博客园
    underscore.js 源码
    Xamarin
  • 原文地址:https://www.cnblogs.com/alipayhutu/p/2498678.html
Copyright © 2011-2022 走看看