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);
}
}
}
标签:XML,SAXReader,System,class,println,import,解析,public 来源: https://blog.csdn.net/m0_60390985/article/details/119244593