XML解析

目录

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

解析XML文件的主流技术包括:DOM、JDOM、SAX、DOM4J;

xpath的对照表

案例


 

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

1.同包:

InputStream in = XmlReader.class.getResourceAsStream("config.xml");

2.跟路径:

InputStream in = XmlReader.class.getResourceAsStream("/config.xml");

3.WIN-INF安全路径:

InputStream in = XmlReader.class.getResourceAsStream("WIN-INF/config.xml");

解析XML文件的主流技术包括:DOM、JDOM、SAX、DOM4J;

这里我们使用的是DOM4J,这是一个非常优秀的开源框架(易用、开源)。使用比较广泛。在使用的过程中、只需要通过查询帮助文档即可完成常规的操作。【当XML文档较大的且不考虑平台移植性时,建议采用DOM4J】

dom4j常用的方法:

方法

描述

selectNodes()拿到多个节点
selectSingleNode()拿到单个节点
attributeValue()返回指定的属性值,如果属性不存在就返回空字符串
getText()拿到文本元素
getRootElement()拿到根元素

xpath的对照表

表达式描述
//表示返回XML文档中的所有符合查找准则的元素,而忽略文档中元素的位置级别
/表示绝对路径
News/Links/name表示相对路径
./表示当前节点
../表示父节点
*表示所有元素

示例:选择一个名为rollno为‘493’值的属性的学生元素

//student[@rollno='493']

案例

配置(导入jar包):

xml配置文件config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config[
	<!ELEMENT config (action*)>
	<!ELEMENT action (forward*)>
	<!ELEMENT forward EMPTY>
	<!ATTLIST action
	  path CDATA #REQUIRED
	  type CDATA #REQUIRED
	>
	<!ATTLIST forward
	  name CDATA #REQUIRED
	  path CDATA #REQUIRED
	  redirect (true|false) "false"
	>
]>
<config>
	<action path="/studentAction" type="org.lisen.mvc.action.StudentAction">
		<forward name="students" path="/students/studentList.jsp" redirect="false"/>
	</action>
</config>

 xml解析代码:

package com.ljq.mymvc.XmlReader;

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;

/**
 * XML解析
 * 
 * @author 一麟
 *
 */
public class XmlReader {

	public static void main(String[] args) throws Exception {
		InputStream in = XmlReader.class.getResourceAsStream("/config.xml");
		SAXReader sax = new SAXReader();
		Document doc = sax.read(in);
		// 获取根元素
		Element rootElement = doc.getRootElement();
		List<Element> actions = rootElement.selectNodes("action");
		for (Element e : actions) {
			String path = e.attributeValue("path");
			String type = e.attributeValue("type");

			System.out.println("action path = " + path);
			System.out.println("action type = " + type);

			List<Element> forwards = e.selectNodes("forward");
			for (Element f : forwards) {
				String name = f.attributeValue("name");
				String fpath = f.attributeValue("path");
				String redirect = f.attributeValue("redirect");

				System.out.println("forward name = " + name);
				System.out.println("forward fpath = " + fpath);
				System.out.println("forward redirect = " + redirect);
			}
			System.out.println("=====================");
		}

	}

}

输出结果如下:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一麟yl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值