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");
        }
    }
  • 相关阅读:
    C# 根据URL获取网页截屏
    Django——WEB应用程序(手写程序),HTTP协议,BS CS架构
    jQuery——标签操作之(样式、文本内容、属性、文档处理)操作
    jQuery——简介,使用
    jQuery下载及应用
    javaScript——案例演示:点击有惊喜
    javaScript——案例演示:弹出模态框
    JavaScript——DOM操作+案例演示
    JavaScript——BOM操作
    JavaScript——杂碎小知识
  • 原文地址:https://www.cnblogs.com/alipayhutu/p/2498678.html
Copyright © 2011-2022 走看看