加载xml文件

<?xml version="1.0" encoding="utf-8" ?>
<class>
    <info>
        <key>jack</key>
        <value>345</value>
    </info>
    <info>
        <key>Bob</key>
        <value>567</value>
    </info>
</class>
package com.dxy.study_0628.study.xml;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.*;

/**
 * Created by lenovo on 2017-6-3.
 */
public class DOMReadDemo {
    public static void main(String[] args) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse("D:\\springmvc\\study_0628\\src\\main\\resources\\xml\\teacher.xml");
            NodeList booklist = document.getElementsByTagName("info");
            for (int i = 0; i < booklist.getLength(); i++) {
                System.out.println("--------第" + (i + 1) + "本书----------");
                Element ele = (Element) booklist.item(i);
                NodeList childNodes = ele.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    Node n = childNodes.item(j);
                    if (n.getNodeName() != "#text") {
                        System.out.println(n.getNodeName() + ":" + n.getTextContent());
                    }
                }
                System.out.println("---------------------------------");
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }

    public void Xml(String path, List<Map<String, String>> lists) throws IOException, SAXException, ParserConfigurationException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(path);
        NodeList booklist = document.getElementsByTagName("info");
        for (int i = 0; i < booklist.getLength(); i++) {
            HashMap<String, String> map = new HashMap<>();
            System.out.println("--------第" + (i + 1) + "本书----------");
            Element ele = (Element) booklist.item(i);
            NodeList childNodes = ele.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node n = childNodes.item(j);
                if (n.getNodeName() != "#text") {
                    System.out.println(n.getNodeName() + ":" + n.getTextContent());
                    map.put(n.getNodeName(), n.getTextContent());
                    lists.add(map);
                }

            }
            System.out.println("---------------------------------");
        }
    }

    /**
     * 测试环境正式环境切换
     * @throws IOException
     * @throws SAXException
     * @throws ParserConfigurationException
     */
    public List<Map<String, String>> Xml_TF() throws IOException, SAXException, ParserConfigurationException {
        String path="";
        List<Map<String, String>> lists = new ArrayList<>();
        ResourceBundle resource = ResourceBundle.getBundle("xml/myconfig");
        String key = resource.getString("flag");
        if ("dev".equals(key)){
            path="src/main/resources/xml/student.xml";
        }else {
            path="src/main/resources/xml/teacher.xml";
        }
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(path);
        NodeList booklist = document.getElementsByTagName("info");
        for (int i = 0; i < booklist.getLength(); i++) {
            HashMap<String, String> map = new HashMap<>();
            Element ele = (Element) booklist.item(i);
            NodeList childNodes = ele.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node n = childNodes.item(j);
                if (n.getNodeName() != "#text") {
                    System.out.println(n.getNodeName() + ":" + n.getTextContent());
                    map.put(n.getNodeName(), n.getTextContent());
                    lists.add(map);
                }

            }
        }
        LinkedHashSet<Map<String, String>> hashSet = new LinkedHashSet<>(lists);
        ArrayList<Map<String, String>> list = new ArrayList<>(hashSet);
        return list;
    }
}

https://www.jb51.net/article/115316.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值