IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> XML解析与练习 -> 正文阅读

[Java知识库]XML解析与练习

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

1:同包?

配置文件和帮助类在同一个包中?

package parse;
//帮助类
import java.io.InputStream;
import java.util.Properties;

/**
 * 配置文件存在位置
 * 1,同包
 * @author T440s
 *
 */
public class Dome {
	public static void main(String[] args) throws Exception{
		//导输入流
		InputStream in=Dome.class.getResourceAsStream("db.properties");
		Properties p=new Properties();
		p.load(in);
		//输出测试一下
		System.out.println(p.getProperty("uname"));
		System.out.println(p.getProperty("upass"));
	}
}

2:根路径

根路径和同包的配置方式是一样的只是加了一个‘ / ’和换到了另一个源文件夹

源文件夹:

package parse;

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

/**
 * 配置文件存在位置
 * 1,同包
 * @author T440s
 *
 */
public class Dome1 {
	public static void main(String[] args) throws Exception{
		//导输入流
		InputStream in=Dome1.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安全路径

WEB-INF 安全目录:放在这个目录下的文件不能直接被外界访问? 只能间接访问

package parse;

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

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

public class DomeServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse arg1) throws ServletException, IOException {
		// TODO Auto-generated method stub
		InputStream in=req.getServletContext().getResourceAsStream("/WEB-INF/db.properties");
		Properties p=new Properties();
		p.load(in);
		//输出测试一下
		System.out.println(p.getProperty("uname"));
		System.out.println(p.getProperty("upass"));
	}
}

二:dom4j的使用

例:

<?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的常用方法:

selectNodes-----获得节点以下所有子节点

selectSingleNode-----获取单个节点信息

attributeValue-----获取属性值

getText-----获取文本内容

package com.sg.parse;

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

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



public class Dome2 {
	//获取小王人名
	public static void main(String[] args) throws Exception{
		/**
		 * 1,读取到students.xml的xml串内容
		 */
		InputStream in=Dome2.class.getResourceAsStream("students.xml");
		SAXReader sr=new SAXReader();
		Document doc=sr.read(in);
		//2,解析获取到所有的student标签的内容
		List<Element> stuEles=doc.selectNodes("/students/student");
		for (Element stuEle : stuEles) {
			//3,获取所有student标签的sid
			String sid=stuEle.attributeValue("sid");
			//4,当sid=s003的时候,获取Name标签内容
			if("s003".equals(sid)) {
				Element nameEle=(Element) stuEle.selectSingleNode("name");
				System.out.println(nameEle.getText());
			}
		}
	}

}

三:Xpath的使用

1;Xpath比dom4j可以更快的获得值

以上面的例

package com.sg.parse;

import java.io.InputStream;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
//求小芳
public class Dome3 {
	public static void main(String[] args) throws Exception{
		InputStream in=Dome3.class.getResourceAsStream("students.xml");
		SAXReader sr=new SAXReader();
		Document doc=sr.read(in);
		Element nameEle=(Element) 
//@定位属性    /定位路径
doc.selectSingleNode("/students/student[@sid='s002']/name");
		System.out.println(nameEle.getText());
	}

}

2;游览器Xpath的使用

第一步:右键打开

?第二步:右键

就复制了游览器Xpath?

四:练习

<?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>
package com.sg.parse;

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

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

public class Dome4 {
	public static void main(String[] args) throws Exception{
		InputStream in=Dome2.class.getResourceAsStream("config.xml");
		SAXReader sr=new SAXReader();
		Document doc=sr.read(in);
		//解析获取到所有的action标签的内容
		List<Element> stuEles=doc.selectNodes("/config/action");
		for (Element stuEle : stuEles) {
			//获取所有type属性的内容
			String type=stuEle.attributeValue("type");
			System.out.println(type);
		}
		System.out.println("--------------");
		//获取第二个type
		String type = stuEles.get(1).attributeValue("type");
		System.out.println(type);
		System.out.println("--------------");
		//获取第二个action下的所有forward的所有path
		Element element = stuEles.get(1);//获取第二个action节点
		List<Element> forwardNodes = element.selectNodes("forward");//获取第二个action下的所有forward节点
		for (Element forwardEl : forwardNodes) {
			String path=forwardEl.attributeValue("path");
			System.out.println(path);
		}
		System.out.println("--------------");
		//获取第二个action下的第二个forward的path
		String path=forwardNodes.get(1).attributeValue("path");
		System.out.println(path);
	}

}

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-07-30 22:41:08  更:2021-07-30 22:41:25 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/2 0:45:11-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码