xml解析详细

xml解析

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

注:*.properties文件 使用key=value 键值对的方式,用 Properties.load()方法获取。

查一组数据 document.selectNodes(xpath);
查单个数据 document.selectSingleNode(xpath);
具体操作看下方代码:

*.properties文件的解析

	public static void main1(String[] args) throws IOException {
		//加载属性配置文件(根据属性配置文件的路径)
		InputStream is = Demo.class.getResourceAsStream("/test.properties");
		//定义属性类
		Properties properties=new Properties();
		properties.load(is);
		
		//取值
		String username = properties.getProperty("username");
		String password = properties.getProperty("password");
		System.out.println(username);
		System.out.println(password);
	}

xml文件解析

public static void main3(String[] args) throws DocumentException {
       //将xml文件变成字节流
		InputStream is = test.class.getResourceAsStream("config.xml");
		SAXReader sax=new SAXReader();
		Document document = sax.read(is);//将其写进文档对象中
		Element e = (Element) document.selectSingleNode("config/action[@path='/regAction']");//获取指定对象
			List<Element> s = e.selectNodes("forward");
			for (Element t : s) {
				 String name = t.attributeValue("name");
					String path = t.attributeValue("path");
					String redirect = t.attributeValue("redirect");
				    ConfigClass con=new ConfigClass();
				    con.setName(name);
				    con.setPath(path);
				    con.setRedirect(redirect);
				    System.out.println(con);
			}
		
		
	}
	
	
	public static void main2(String[] args) throws DocumentException {
		
		InputStream is = test.class.getResourceAsStream("config.xml");
		SAXReader sax=new SAXReader();
		Document document = sax.read(is);
		List<Element> e =(List<Element>)document.selectNodes("config/action[@path='/regAction']");
		for (Element el : e) {
			 List<Element> n =el.selectNodes("forward");
			 for (Element t : n) {
				 String name = t.attributeValue("name");
					String path = t.attributeValue("path");
					String redirect = t.attributeValue("redirect");
					 System.out.println(name+"--"+path+"--"+redirect);
					 
			}
			 String att = el.attributeValue("path");
			 String att1 = el.attributeValue("type");
			 System.out.println(att+"--"+att1);
		}
	}
	
	 public static void main1(String[] args) throws DocumentException {
		 InputStream is = test.class.getResourceAsStream("config.xml");
		 SAXReader sax=new SAXReader();
		 Document document = sax.read(is);
		 List<Element> list = document.selectNodes("config/action");
	     for (Element e : list) {
			Element n=(Element)e.selectSingleNode("forward");
		    String name = n.attributeValue("name");
			String path = n.attributeValue("path");
			String redirect = n.attributeValue("redirect");
			System.out.println(name+"--"+path+"--"+redirect);
			
			String att = e.attributeValue("path");
			String att1 = e.attributeValue("type");
			System.out.println(att+"--"+att1);
		}
	 
	 }



**今天的分享就到此结束,各位看官可以在下面评论出自己的看法,大家一起提升,谢谢各位!!!**
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值