xml解析

xml的作用,

可以作为数据传输的载体,配置

获取指定位置下的资源文件:
读取指定下的资源文件:

1,读取同包下的资源文件
获取到同包下的资源文件,将其转化为流对象

InputStream in = PropertiesDemo.class.getResourceAsStream("db.properties");

专门的工具类来讲流中的数据解析
拿取uname与upass

Properties p=new Properties();
		p.load(in);
		System.out.println(p.getProperty("uname"));
		System.out.println(p.getProperty("upass"));

2,资源文件存放在根目录下
2.1,在src下建一个Source Floder文件夹,将文件导入,
2.1.1source folder的意思:代码不将其当作文件夹来处理,程序猿用来做文件归类所用
2.2,拿取uname与upass

InputStream in = PropertiesDemo.class.getResourceAsStream("/db.properties");
		Properties p=new Properties();
		p.load(in);
		System.out.println(p.getProperty("uname"));
		System.out.println(p.getProperty("upass"));
		

3资源文件存放在web-inf下

package com.hutao;

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

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

public class parseServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@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 {
		// TODO Auto-generated method stub
		ServletContext context = req.getServletContext();
		InputStream in = context.getResourceAsStream("/WEB-INF/db.properties");
		Properties p=new Properties();
		p.load(in);
		System.out.println(p.getProperty("uname"));
		System.out.println(p.getProperty("upass"));
	}

}
xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>f</display-name>
<servlet>
<servlet-name>parseServlet</servlet-name>
<servlet-class>com.hutao.parseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>parseServlet</servlet-name>
<url-pattern>/parseServlet</url-pattern>
</servlet-mapping>
</web-app>

4,解析指定路径下的资源文件
拿到资源文件,

public static void main(String[] args) throws DocumentException {
		InputStream in = xmlDemo.class.getResourceAsStream("students.xml");
		SAXReader saxReader=new SAXReader();
		Document read = saxReader.read(in);
		System.out.println(read.asXML());
		
	}
xpath解析,xpath解析能够将xml格式的串当作目录结构来进行查找
方法1	
		List<Element> stuEles = read.selectNodes("/students/student");
		for (Element stuEle : stuEles) {
			if ("s002".equals(stuEle.attributeValue("sid"))) {
				System.out.println(stuEle.asXML());
				Element nameEle = (Element)stuEle.selectSingleNode("name");
				System.out.println(stuEle.asXML());
				System.out.println(nameEle.getText());
			}
		}
方法2
Element stus002Ele = (Element)read.selectSingleNode("/students/student[@sid='s002']");
		System.out.println(stus002Ele.asXML());

作业:

package com.hutao;

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

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

/**
 * 1、获取所有action中的type的值
   2、获取第二个action中的type的值
   3、获取第二个action的所有forward的path
   4、获取第二个action的第二个forward的path

 * @author 2019040901
 *
 */
public class zuoye {
	public static void main(String[] args) throws Exception {
		//获取所有action中的type的值
		InputStream in = zuoye.class.getResourceAsStream("config.xml");
		SAXReader reader = new SAXReader();
		Document doc = reader.read(in);
//		1、获取所有action中的type的值
		List<Element> stuElems = doc.selectNodes("/config/action");
		for (Element stuEle : stuElems) {
			String type = stuEle.attributeValue("type");
			System.out.println(type);
		} 
//		 2、获取第二个action中的type的值
		List<Node> stuElems = doc.selectNodes("/config/action[@path='/loginAction']");
		for (Node node : stuElems) {
			Element a= (Element) node;
            String type=a.attributeValue("type");
            System.out.println(type);
		}
//		3、获取第二个action的所有forward的path
		List<Node> stu = doc.selectNodes("/config/action[@type='test.LoginAction']/forward");
		for (Node node : stu) {
			Element a= (Element) node;
            String path=a.attributeValue("path");
            System.out.println(path);
		}
//		 4、获取第二个action的第二个forward的path
        List<Node> stu = doc.selectNodes("/config/action[@type='test.LoginAction']/forward[@name='success']");
		for (Node node : stu) {
			Element a= (Element) node;
            String path=a.attributeValue("path");
            System.out.println(path);
		}
	}
}

xml文件

<?xml version="1.0" encoding="UTF-8"?>
	<!--
		config标签:可以包含0~N个action标签
	-->
<config>
	<!--
		action标签:可以饱含0~N个forward标签
		path:以/开头的字符串,并且值必须唯一 非空
		type:字符串,非空
	-->
	<action path="/regAction" type="test.RegAction">
		<!--
			forward标签:没有子标签; 
			name:字符串,同一action标签下的forward标签name值不能相同 ;
			path:以/开头的字符串
			redirect:只能是false|true,允许空,默认值为false
		-->
		<forward name="failed" path="/reg.jsp" redirect="false" />
		<forward name="success" path="/login.jsp" redirect="true" />
	</action>

	<action path="/loginAction" type="test.LoginAction">
		<forward name="failed" path="/login.jsp" redirect="false" />
		<forward name="success" path="/main.jsp" redirect="true" />
	</action>
</config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值