栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2.编写java框架的必备基础-使用dom4j读取xml文件-需要对应视频教程-可找我领取

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2.编写java框架的必备基础-使用dom4j读取xml文件-需要对应视频教程-可找我领取



	
	
	
		
	
	
	
	
		
	
	
	
	
		
	
	
	
	
		
		
	
  

如果你了解spring,我相信你应该知道这是spring的配置文件。接下来,我们将自己使用dom4j读取这个配置文件,并解析出里面相关的内容来。

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.document;
import org.dom4j.documentException;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;


public class Main {
	public static void main(String[] args) throws documentException {
		SAXReader reader = new SAXReader();
		document document = reader.read(Main.class.getClassLoader()
				.getResourceAsStream("applicationContext.xml"));
		
		Map nsMap = new HashMap();
		nsMap.put("ns", "http://www.springframework.org/schema/beans");
		
		XPath xsub = document.createXPath("//ns:beans/ns:bean");
		xsub.setNamespaceURIs(nsMap);
		
		List beans = xsub.selectNodes(document);
		for (Element bean : beans) {
			String id = bean.attributevalue("id");
			String clazz = bean.attributevalue("class");
			String scope = bean.attributevalue("scope");
			
			System.out.println("id:" + id + ",class:" + clazz + ",scope:" + scope);
			List properties = bean.elements();
			for (Element proerty : properties) {
				String name = proerty.attributevalue("name");
				String ref = proerty.attributevalue("ref");
				System.out.println("name:" + name + ",ref:" + ref);
			}
			System.out.println("======================================");
		}
	}
}

程序运行效果如下图所示,我们可以看到,使用dom4j已经顺利地解析出了xml配置文件里面的内容。

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/324635.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号