XML解析

XML解析

 xml文件将变动的代码变成可配置的文件,用来维护可变动的代码

1.同包(不加/代表同类名包下的相对路径)

2.根路径(/代表获取src根目录的绝对路径)

test.properties为src根目录下的文件,故获取路径是需要   /

test.properties在com.ltf.xml包中,即与Demo1同包,则获取路径时不需要 /

(properties文件是资源文件中的一种)

package com.LSZ.xml;

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

public class demo1 {

	public static void main(String[] args) throws IOException {
		// 如果是properties文件 Properties类 xml文件
		InputStream IS = demo1.class.getResourceAsStream("db.properties");
		System.out.println(IS);
		// 得到解析器
		Properties ps = new Properties();
		ps.load(IS);
		String name = ps.getProperty("name");
		String password = ps.getProperty("password");
		System.out.println(name + ":" + password);

	}

}

 WEB-INF安全路径(context.getResourceAsStream("/WEB-INF/test.properties");

package com.LSZ.xml;

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

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("/demo")
public class demo2 extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		InputStream IS = req.getServletContext().getResourceAsStream("/WEB-INF/test.properties");
		Properties p = new Properties();
		p.load(IS);
		System.out.println(p.getProperty("name"));
		System.out.println(p.getProperty("password"));
	}

}

 dom4j常用方法

1.selectNodes()         获取节点下所有子节点;

2.selectSingleNode()获取单个节点信息;

3.attributeValue()      获取指定对象的属性值

4.getText()                 获取对应节点对象的内容

package com.LSZ.xml;

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;

public class demo3 {
	public static void main(String[] args) throws Exception {

		InputStream in = demo3.class.getResourceAsStream("/students.xml");
		SAXReader sr = new SAXReader();
		Document doc = sr.read(in);

		List<Element> stuEles = doc.selectNodes("/students/student");
		for (Element stuEle : stuEles) {

			String sid = stuEle.attributeValue("sid");
			// attributeValue: 获取指定对象的属性值

			if ("s003".equals(sid)) {
				Element nameEle = (Element) stuEle.selectSingleNode("name");
				// selectSingleNode:获取对应节点对象 返回单个元素对象
				System.out.println(nameEle.getText());
			}
		}

	}

}

xpath的使用

首先导入jaxen-1.1-beta-6.jar jar包, xpath解析是基于dom4j 的;所以还需要导入dom4j jar包

 语法:/ 定位路径 @属性

package com.LSZ.xml;

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;

public class demo4 {

	public static void main(String[] args) throws DocumentException {
		InputStream in = demo4.class.getResourceAsStream("students.xml");
		SAXReader sr = new SAXReader();
		Document red = sr.read(in);

		Element name = (Element) red.selectSingleNode("/students/student[@sid='s002']/name");
		System.out.println("名字:" + name.getText());
	}

}

案例:

<?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.LSZ.xml;

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;

public class demo5 {
	public static void main(String[] args) throws DocumentException {

		InputStream in = demo5.class.getResourceAsStream("config.xml");
		SAXReader sr = new SAXReader();
		Document red = sr.read(in);// 读取流中的数据
		// 1.获取所有action中的type的值
		List<Element> seleNode = red.selectNodes("/config/action");
		for (Element element : seleNode) {
			String type = element.attributeValue("type");
			System.out.println("所有action中的type的值:" + type);
		}

		// 获取第二个action中的type的值

		List<Element> Nodes = red.selectNodes("/config/action[@path='/loginAction']");
		for (Element element : Nodes) {
			String type = element.attributeValue("type");
			System.out.println("第二个action中的type的值:" + type);
		}

		// 获取第二个action的所有forward的path
		List<Element> Nodess = red.selectNodes("/config/action[@path='/loginAction']/forward");
		for (Element element : Nodess) {
			String path = element.attributeValue("path");
			System.out.println("第二个action的所有forward的path" + path);
		}
		// 获取第二个action的第二个forward的path

		List<Element> selectNodes = red.selectNodes("/config/action[@path='/loginAction']/forward[@name='success']");
		for (Element element : selectNodes) {
			String path = element.attributeValue("path");
			System.out.println("第二个action的第二个forward的path:" + path);
		}

	}

}

上一篇:文件类型


下一篇:xml相关操作