XML解析

  1. Java中配置文件的三种配置位置及读取方式
    1.1 XML和*.properties(属性文件)/ini
    1.2 存放位置
    1.2.1 src根目录下
    Xxx.class.getResourceAsStream("/config.properties");
    1.2.2 与读取配置文件的类在同一包
    Xxx.class.getResourceAsStream(“config2.properties”);
    1.2.3 WEB-INF(或其子目录下)、
    ServletContext application = this.getServletContext();
    InputStream is =
    application.getResourceAsStream("/WEB-INF/config3.properties");

2

  //通过某个类xxx.class来读取指定的配置文件
    	  InputStream is = Dame.class.getResourceAsStream("/config.xml");
    	  //调用外部架包
    	  SAXReader   sa=new  SAXReader();
    	  try {
			Document read = sa.read(is);
			//找到config.xml,action节点
			List<Element> selectNodes = read.selectNodes("/config/action");
			for (Element n : selectNodes) {
				//得到属性的内容
				String path = n.attributeValue("path");
				String type = n.attributeValue("type");
				System.out.println("action:"+path+","+type);
				//循环嵌套,根据config.xml,action节点上在添加一个节点
				List<Element> selectNodes2 = n.selectNodes("forward");
				for (Element s : selectNodes2) {
						String name = s.attributeValue("name");
						String path1 = s.attributeValue("path");
						String redirect = s.attributeValue("redirect");
						System.out.println("forward:"+name+","+path1+","+redirect);
				}
			}
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  1. dom4j+xpath解析xml文件
    dom4j是一个外部的工具类
    xpath等同数据库的select语句

document.selectNodes(xpath);//查一组
document.selectSingleNode(xpath);//查单个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值