java读取xml配置文件

1.使用dom4j读取

    <dependency>
            <groupId>dom4jgroupId>
            <artifactId>dom4jartifactId>
            <version>1.6.1version>
        dependency>

2.xml在src/main/resources文件夹下

//初始化
    public void readXml() throws DocumentException{
    String LOCAL_LIST_PATH = this.getClass().getClassLoader().getResource("/xxx.xml").getPath();
        //1.读取
        SAXReader reader = new SAXReader();
        Document document = reader.read(LOCAL_LIST_PATH);       
        //2.获得根元素
        Element rootElement =  document.getRootElement();   
        Iterator it =  rootElement.elementIterator();
        Element ele = null;
        while(it.hasNext()){
            ele = (Element)it.next();

        }
    }

你可能感兴趣的:(问题记录集)