XML解释:
可扩展标记语言,标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言。
在电子计算机中,标记指计算机所能理解的信息符号,通过此种标记,计算机之间可以处理包含各种的信息比如文章等。它可以用来标记数据、定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言。 它非常适合万维网传输,提供统一的方法来描述和交换独立于应用程序或供应商的结构化数据。是Internet环境中跨平台的、依赖于内容的技术,也是当今处理分布式结构信息的有效工具。早在1998年,W3C就发布了XML1.0规范,使用它来简化Internet的文档信息传输。
内容:
- Java中配置文件的三种配置位置及读取方式
1.1 XML和*.properties(属性文件)/ini
1.2 存放位置
1.2.1 src根目录下
Xxx.class.getResourceAsStream("/config.properties");
1.2.2 与读取配置文件的类在同一包
Xxx.class.getResourceAsStream(“config2.properties”);
1.2.3 WEB-INF(或其子目录下)
ServletContext application = this.getServletContext();
InputStream is =
application.getResourceAsStream("/WEB-INF/config3.properties");
注1:*.properties文件
key=value
#注释
Properties.load(is)
-
XML的作用
配置
*.properties
*.xml
*.ini数据交换
xml
webservice
json
3.创建一个lib的包然后将
dom4j+xpath解析xml文件放入其中
在导一下包
xpath等同数据库的select语句
```java
// 通过某个类.class来读取指定的配置文件
InputStream stream = Config.class.getResourceAsStream("/config.xml");
//得到saxreader对象
SAXReader s=new SAXReader();
//调用saxreader对象的读取方法read();
Document read = s.read(stream);
List<Element> selectNodes = read.selectNodes("/config/action");
for (Element e : selectNodes) {
String path = e.attributeValue("path");
String type = e.attributeValue("type");
System.out.println("action[path="+path+" ,type="+type);
List<Element> selectNodes2 = e.selectNodes("forward");
for (Element e2 : selectNodes2) {
String name = e2.attributeValue("name");
String forwardpath = e2.attributeValue("path");
String redirect = e2.attributeValue("redirect");
System.out.println("name="+name+",path="+forwardpath+",redirect="+redirect);
}
}
document.selectNodes(xpath);//查一组
document.selectSingleNode(xpath);//查单个
DOM由节点组成
Node
元素节点
属性节点
文本节点
xpath
/ 定位路径 在系统中建一个文件叫document/students/student/sid|name
@ 属性
举例:/students/student[@pid='p02']
students.xml