XML解析

思维导图

一  java中配置文件的三种配置位置及其读取方法

1.读取根路径下的资源文件

package com.xyl.parse;

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


public class Demo1 {
    public static void main(String[] args) throws Exception {
        InputStream in = Demo1.class.getResourceAsStream("/db.properties");
        Properties p = new Properties();
        p.load(in);
        System.out.println(p.getProperty("uname"));
        System.out.println(p.getProperty("upass"));

 

    }
}

properties文件要放到src目录下,所以要加/,不然会报错

2、读取同包下的资源文件

package com.xyl.parse;

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


public class Demo2 {
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        InputStream in = Demo2.class.getResourceAsStream("db.properties");
        Properties p = new Properties();
        p.load(in);
        System.out.println(p.getProperty("uname"));
        System.out.println(p.getProperty("upass"));
    }
}

 

3、读取WEB-INF下的资源文件

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // TODO Auto-generated method stub
    ServletContext context = req.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"));

 }

}

运行结果 :

二、dom4j的使用

四个常用方法:

selectNodes

selectSingleNode

attributeValue

getText

例题:

xml文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<students>
    <student sid="s001">
        <name>小明</name>
    </student>
    <student sid="s002">
        <name>小芳</name>
    </student>
    <student sid='s003'>
        <name>小王</name>
    </student>
</students>

 
dom4j解析方式
 
package com.xyl.xml;
 
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
public class Demo3 {
    public static void main(String[] args) throws DocumentException{
        InputStream in = Demo3.class.getResourceAsStream("students.xml");
        SAXReader sr=new SAXReader();
        Document dc=sr.read(in);
        //System.out.println(dc.asXML());读出界面所有的内容
        List<Element> list=dc.selectNodes("/students/student");
        for (Element element : list) {
            String sid=element.attributeValue("sid");
            if("s003".equals(sid)) {
                //selectSingleNode 获得对应节点对象 返回单个元素对象
                Element name=(Element)element.selectSingleNode("name");
                System.out.println(name.getText());
            }
        }
    }
}

三.xpath解析方式

/定位路径

@属性

package com.xyl.com;
 
import java.io.InputStream;
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
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 dc=sr.read(in);
        Element name=(Element)dc.selectSingleNode("/students/student[@sid='s002']/name");
        System.out.println(name.getText());
    }
}

练习案例:

1,获取所有action中的type的值

public class Dome4 {
public static void main(String[] args) throws Exception {
    InputStream is=Dome4.class.getResourceAsStream("config.xml");
    SAXReader sr=new SAXReader();
    Document doc = sr.read(is);
    // 获取对应节点对象
List<Element> list = doc.selectNodes("/config/action");
for (Element element : list) {
    //获取指定对象的属性值
    String types = element.attributeValue("type");
    System.out.println(types);
}
}
}
 

2,获取第二个action中的type值
 

public class Dome4 {
public static void main(String[] args) throws Exception {
InputStream is = Dome4.class.getResourceAsStream("config.xml");
SAXReader sr = new SAXReader();
Document doc = sr.read(is);
// 获取对应节点对象
List<Element> Elements = doc.selectNodes("/config/action");
// 获取指定对象的属性值
String type =Elements.get(1).attributeValue("type");
System.out.println(type);
}
}

 

3,获取第二个action的所有forward的path

public class Dome4 {
    public static void main(String[] args) throws Exception {
        InputStream is = Dome4.class.getResourceAsStream("config.xml");
        SAXReader sr = new SAXReader();
        Document doc = sr.read(is);
        // 获取对应节点对象
        List<Element> Element = doc.selectNodes("/config/action/forward");
        for (Element ele : Element) {
        //获取指定对象的属性值
            String path = ele.attributeValue("path");
            System.out.println(path);
        }
    }
}

 

4,获取第二个action的第二个forward的path

public class Dome4 {
    public static void main(String[] args) throws Exception {
        InputStream is = Dome4.class.getResourceAsStream("config.xml");
        SAXReader sr = new SAXReader();
        Document doc = sr.read(is);
         List<Element> Elements = doc.selectNodes("/config/action");
        // 获取对应节点对象
        List<Element> Elementss = Elements.get(1).selectNodes("forward");
        for (Element eless : Elementss) {
        //获取指定对象的属性值
            String path = eless.attributeValue("path");
            System.out.println(path);
        }
    }
}

 
 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值