java XML解析

1. 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)


2. XML的作用
   配置
     *.properties
     *.xml
     *.ini
   数据交换
     xml
       webservice
     json 


3. dom4j+xpath解析xml文件
   xpath等同数据库的select语句

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

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

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

 

 

 

代码案例: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    
      path CDATA  #REQUIRED
      redirect (false|true)  #REQUIRED
      name  CDATA #REQUIRED
      >
]>


<!--
        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>

 

解析代码

public static void main(String[] args) {
        
        //XML解析
        InputStream is= Demo.class.getResourceAsStream("/tasd.xml");
        SAXReader sa=new SAXReader();
    
    try {
        Document read = sa.read(is);
        //找到tasd节点
        
        List<Element> actionElementList = read.selectNodes("/config/action");
        for (Element element : actionElementList) {
            String path = element.attributeValue("path");
            String type = element.attributeValue("type");
            System.out.println("action[path="+path+",type="+type+"]");
        List<Element> tasdNode = element.selectNodes("forward");
    for (Element e : tasdNode) {
        //得到节点属于的内容
        String forwardpath = e.attributeValue("path");
        String redirect = e.attributeValue("redirect");
        String name = e.attributeValue("name");
    
        System.out.println("forward[name="+name+"path="+forwardpath+",redirect="+redirect+"]");
    }
    
        }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值