XML解析

XML解析

java中配置文件的三种配置位置及读取方式

db.properties文件

uname=zhangsw
upass=123
url=jdbc:mysql://localhost:3306/mybatis_ssm
driver_Class=com.mysql.jdbc.Driver

initPoolSize=5
maxPoolSize=20

1.读取根路径下的资源文件
加载根路径下的资源文件db.properties文件,到jvm称为流对象,"/"代表的是根路径->跟指的是src source folder->物理将代码分层, jvm不会读取这个文件夹

package com.zking;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Demo1 {
	public static void main(String[] args) throws IOException {
		InputStream in=Demo1.class.getResourceAsStream("/db.properties");
//		java.utils.properties
		Properties p =new Properties();
//		此时p中就加载了流对象,从而就有了该资源文件的所有配置信息
		p.load(in);
		System.out.println(p.getProperty("uname"));
		System.out.println(p.getProperty("upass"));
	}
}

运行结果:

2、读取同包下的资源文件
XML解析_第1张图片

package com.zking;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 
 * @author zhangsw
 *
 *读取同包下的资源文件
 */
public class Demo2 {
	public static void main(String[] args) throws IOException {
		InputStream in=Demo2.class.getResourceAsStream("db.properties");
		Properties p =new Properties();
//		此时p中就加载了流对象,从而就有了该资源文件的所有配置信息
		p.load(in);
		System.out.println(p.getProperty("uname"));
		System.out.println(p.getProperty("upass"));
	}
}

运行结果:
XML解析_第2张图片
3、读取WEB-INF下的资源文件

package com.zking;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="demo",urlPatterns="/demo")
public class DemoServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ServletContext context = req.getServletContext();
		InputStream in = context.getResourceAsStream("/WEB-INF/db.properties");
		Properties p = new Properties();
//		此时p中就加载了流对象,从而就有了该资源文件的所有配置信息
		p.load(in);
		System.out.println(p.getProperty("uname"));
		System.out.println(p.getProperty("upass"));
		
	}
}

运行结果:
XML解析_第3张图片

dom4j+xpath解析xml文件
xml文件内容

<?xml version="1.0" encoding="UTF-8"?>
<students>
	<student sid="s001">
		<name>小明</name>
	</student>
	<student sid="s002">
		<name>小芳</name>
	</student>
	<student sid='s003'>
		<name>小王</name>
	</student>
</students>

dom4j解析方式

package com.zking;

import java.io.InputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
 * dom4j+xpath解析xml
 * @author zhangsw
 *
 */
public class Demo3 {
	public static void main(String[] args) throws DocumentException {
		// TODO Auto-generated method stub
		InputStream in= Demo3.class.getResourceAsStream("/students.xml");
		SAXReader reader=new SAXReader();
		Document document =reader.read(in);
		List<Element>stuEles= document.selectNodes("/students/student");
		for (Element stuEle : stuEles) {
			if("s002".equals(stuEle.attributeValue("sid"))){
				Element nameEle=(Element)stuEle.selectSingleNode("name");
				System.out.println("sid="+stuEle.attributeValue("sid")+",name="+nameEle.getText());
				}
		}
		
		
	}
}

xpath解析方式

package com.zking;

import java.io.InputStream;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
/**
 * 
 * @author zhangsw
 *
 */
public class Demo4 {
	public static void main(String[] args) throws DocumentException {
		InputStream in= Demo3.class.getResourceAsStream("/students.xml");
		SAXReader reader=new SAXReader();
		Document document =reader.read(in);
		Element stuEles002 = (Element)document.selectSingleNode("/students/student[@sid='s002']");
		Element nameEle =(Element) stuEles002.selectSingleNode("name");
	System.out.println("sid="+stuEles002.attributeValue("sid")+",name="+nameEle.getText());
		
	}
}

总结:
xpath相较于dom4j解析来说,更加的方便,因为xpath可以获取指定元素的指定属性。

你可能感兴趣的:(XML解析)