xml解析

本文介绍了Java中配置文件的三种加载位置及其读取方式,包括同包、根路径和WeB-INF安全目录。同时,详细讲解了dom4j库的使用,如selectNodes、attributeValue、selectSingleNode和getText等方法,并通过实例展示了XPath的运用。最后,给出了四个练习题,涉及从XML文件中提取特定元素的属性值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、学习导图

 二、Java中配置文件的三种配置位置及读取方式
1、同包 :Dhm01.class.getResourceAsStream("db.properties");
2、根路径相同 :Dhm01.class.getResourceAsStream("/db.properties");

 

例:

package parse;

import java.io.InputStream;
import java.util.Properties;
/**
 * 配置文件存在位置
 * 1、同包
* WeB-INF 安全目录:凡是放在这个目录的文件不能直接被外界访问
 * @author T440s
 *
 */
public class Dhm01 {
    public static void main(String[] args) throws Exception{
//同包        InputStream in=Dhm01.class.getResourceAsStream("db.properties");

//  根路径相同      InputStream in=Dhm01.class.getResourceAsStream("/db.properties");
        Properties p=new Properties();
        p.load(in);
        System.out.println(p.getProperty("uname"));
    }
}

 

3、WeB-INF 安全目录:凡是放在这个目录的文件不能直接被外界访问
间接访问方式:context.getResourceAsStream("/WeB-INF/db.properties");

package parse;

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

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/demo")
public class Dhmserverlet extends HttpServlet{
    protected void service(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        InputStream in=req.getServletContext().getResourceAsStream("/WeB-INF/db.properties");
        Properties p=new Properties();
        p.load(in);
        System.out.println(p.getProperty("uname"));
}
}

 

三、dom4j的使用
四种常用方法:

selectNodes        获取对应节点对象,返回list

attributeValue        获取指定对象的属性

selectSingleNode        获取对应节点对象

getText

例如:

package 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 Dhm02 {
     public static void main(String[] args) throws DocumentException {
         InputStream in=Dhm02.class.getResourceAsStream("students.xml");
         SAXReader sr=new SAXReader();
         Document doc=sr.read(in);
//         selectNodes获取对应节点对象,返回list
         List<Element> stuEles=doc.selectNodes("/students/student");
         for (Element stuEle : stuEles) {
//             attributeValue获取指定对象的属性
            String sid=stuEle.attributeValue("sid");
            if(sid.equals("s003")) {
//                selectSingleNode获取对应节点对象
                Element a=(Element) stuEle.selectSingleNode("name");
                System.out.println(a.getText());
            
            }
        }
    }
}
四、xpath的使用
语法:

/   定位路径           @  属性

案例:

package parse;
import java.io.InputStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
public class Dhm03 {
    public static void main(String[] args) throws DocumentException {
        InputStream in=Dhm03.class.getResourceAsStream("students.xml");
        SAXReader sr=new SAXReader();
        Document doc=sr.read(in);
        Element nameEle= (Element) doc.selectSingleNode("/students/student[@sid='s002']/name");
    System.out.println(nameEle.getText());
    }
}
五、练习
1、获取所有config中type的值
package 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;
/**
 * 获取所有config中type的值
 * @author T440s
 *
 */
public class Dhmzy01 {
    public static void main(String[] args) throws DocumentException {
         InputStream in=Dhmzy01.class.getResourceAsStream("config.xml");
         SAXReader sr=new SAXReader();
         Document doc=sr.read(in);
         List<Element> stuEles=doc.selectNodes("/config/action");
         
         for (Element ele : stuEles) {
             String type=ele.attributeValue("type");
             System.out.println(type);
        }
         
    }
 
}
2、获取第二个config中type的值
package 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;
/**
 * 获取第二个config中type的值
 * @author T440s
 *
 */
public class Dhmzy02 {
    public static void main(String[] args) throws DocumentException {
         InputStream in=Dhmzy02.class.getResourceAsStream("config.xml");
         SAXReader sr=new SAXReader();
         Document doc=sr.read(in);
         List<Element> stuEles=doc.selectNodes("/config/action");
         Element ele=stuEles.get(1);
             String type=ele.attributeValue("type");
             System.out.println(type);
    }
}
3、获取第二个config中所有forward的path
package 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;
/**
 * 获取第二个config中所有forward的path
 * @author T440s
 *
 */
public class Dhmzy03 {
    public static void main(String[] args) throws DocumentException {
        InputStream in=Dhmzy03.class.getResourceAsStream("config.xml");
        SAXReader sr=new SAXReader();
        Document doc=sr.read(in);
        List<Element> list= doc.selectNodes("/config/action[@path='/loginAction']/forward");
         for (Element element : list) {
              String path=element.attributeValue("path");
             System.out.println(path);
        }
       
    }
    
 
}
 4、获取第二个config中第二个forward的path
package 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;
/**
 * 获取第二个config中第二个forward的path
 * @author T440s
 *
 */
public class Dhmzy04 {
    public static void main(String[] args) throws DocumentException {
        InputStream in=Dhmzy03.class.getResourceAsStream("config.xml");
            SAXReader sr=new SAXReader();
            Document doc=sr.read(in);
           List<Element> list= doc.selectNodes("/config/action[@path='/loginAction']/forward");
           Element ele=list.get(1);
        String path=ele.attributeValue("path");
         System.out.println(path);
    }
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值