XML是一种通用的数据交换格式,它的平台无关性、语言无关性、系统无关性、给数据集成与交互带来了极大的方便。XML在不同的语言环境中解析方式都是一样的,只不过实现的语法不同而已。
XML的解析方式分为四种:1、DOM解析;2、SAX解析;3、JDOM解析;4、DOM4J解析。其中前两种属于基础方法,是官方提供的平台无关的解析方式;后两种属于扩展方法,它们是在基础的方法上扩展出来的,只适用于java平台。
1. Java中配置文件的三种配置位置及读取方式
1.1 XML和*.properties(属性文件)
1.2 存放位置
1.2.1 src根目录下
将资源文件放在跟目录下, 切记一定是src源文件夹(Source Folder):
public static void main(String[] args) throws IOException {
// 获取到同包下的资源文件,将其转化成流对象
// InputStream in = PropertiesDemo.class.getResourceAsStream("db.properties");
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"));
}
输出的结果为:
1.2.2 与读取配置文件的类在同一包
我们通过以下代码读取到指定位置下的*.properties资源文件
资源文件如图:
public static void main(String[] args) throws IOException {
// 获取到同包下的资源文件,将其转化成流对象
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"));
}
输出的结果为:
1.2.3 WEB-INF(或其子目录下)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext context = request.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"));
}
输出的结果为:
注1:*.properties文件
key=value
#注释
Properties.load(is)
2.XML作用
1.1配置
.xml和.propertie
1.2数据交互(获取第三方数据)
XML:webservices(axis2)restful webservices 基于http协议的方式JSON
3. dom4j+xpath解析xml文件
xpath等同数据库的select语句
document.selectNodes(xpath);//查一组
document.selectSingleNode(xpath);//查单个
package com.xieminglu.parse;
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 XmlDemo {
public static void main(String[] args) throws Exception {
InputStream in = XmlDemo.class.getResourceAsStream("students.xml");
SAXReader saxReader=new SAXReader();
Document doc=saxReader.read(in);
System.out.println(doc.asXML());
// xpath解析能够将xml格式的串当作目录结构来进行查找
// 第一种:
List<Element> stuEles = doc.selectNodes("/students/student");
for (Element stuEle : stuEles) {
if("s002".equals(stuEle.attributeValue("sid"))) {
System.out.println(stuEle.asXML());
Element nameEles=(Element)stuEle.selectSingleNode("name");
System.out.println(nameEles.asXML());
}
}
// 第二种:
Element stuS002Ele=(Element)doc.selectSingleNode("/students/student[@sid='s002']");
System.out.println(stuS002Ele.asXML());
}
}
最终所输出的结果为:
作业:config.xml解析
1、获取所有action中的type的值
2、获取第二个action中的type的值
3、获取第二个action的所有forward的path
4、获取第二个action的第二个forward的path
package com.xieminglu.parse;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class ConfigDemo {
public static void main(String[] args) throws Exception {
InputStream in = ConfigDemo.class.getResourceAsStream("config.xml");
SAXReader saxReader=new SAXReader();
Document doc=saxReader.read(in);
System.out.println("1、获取所有action中的type的值");
List<Node> list = doc.selectNodes("/config/action");
for (Node node : list) {
Element el=(Element) node;
//1.以属性名获取属性值
String a = el.attributeValue("type");
System.out.println(a);
}
System.out.println("2、获取第二个action中的type的值");
List<Node> list1 = doc.selectNodes("/config/action[@path='/loginAction']");
for (Node node : list1) {
Element el=(Element) node;
//1.以属性名获取属性值
String a = el.attributeValue("type");
System.out.println(a);
}
System.out.println("3、获取第二个action的所有forward的path");
List<Node> list2 = doc.selectNodes("/config/action[@path='/loginAction']/forward");
for (Node node : list2) {
Element el=(Element) node;
//1.以属性名获取属性值
String a = el.attributeValue("path");
System.out.println(a);
}
System.out.println("4、获取第二个action的第二个forward的path");
List<Node> list3 = doc.selectNodes("/config/action[@path='/loginAction']/forward[@name='success']");
for (Node node : list3) {
Element el=(Element) node;
//1.以属性名获取属性值
String a = el.attributeValue("path");
System.out.println(a);
}
}
}
输出的结果为: