XML解析

1. Java中配置文件的三种配置位置及读取方式
1.1 XML和*.properties(属性文件)/ini
1.2 存放位置
1.2.1 src根目录下
Xxx.class.getResourceAsStream("/config.properties");



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 test2 {
public static void main(String[] args) {
	InputStream is = test2.class.getResourceAsStream("/config.xml");
}
}
1.2.2 与读取配置文件的类在同一包
      Xxx.class.getResourceAsStream("config2.properties");
package lib;


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 test2 {
public static void main(String[] args) {
	InputStream is = test2.class.getResourceAsStream("config.xml");
}
}

1.2.3 WEB-INF(或其子目录下)
ServletContext application = this.getServletContext();
InputStream is =
application.getResourceAsStream("/WEB-INF/config3.properties");
示例:略
注1:*.properties文件
key=value
#注释
Properties.load(is)
2. XML的作用
配置(三种配置文件)
*.properties
*.xml
*.ini
数据交换
xml
webservice
json (现在主要流行的)
3. dom4j+xpath解析xml文件
dom4j是一个外部的工具类
xpath等同数据库的select语句

document.selectNodes(xpath);//查一组
document.selectSingleNode(xpath);//查单个

DOM由节点组成
Node
元素节点
属性节点
文本节点

xpath
/ 定位路径 在系统中建一个文件叫document/students/student/sid|name
@ 属性
举例:/students/student[@pid=‘p02’]
students.xml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值