Xml解析需要依赖 Dom4j包 备注:记得导入Dom4j包
XML的作用
配置
*.properties
userPage=20
*.xml
数据交换
xml
webservice
json
静态资源文件
css、js、html、图片、视屏
读取指定位置下的资源文件(db.properties)
//获取同包下的资源文件,将其转化成流对象 InputStream in=PropertiesDemo.class.getResourceAsStream("db.properties");
需要专门的工具类来将流中的数据解释出
Properties p=new Properties(); p.load(in); System.out.println(p.getProperty("uname")); System.out.println(p.getProperty("upass"));
资源文件存放在根目录下
InputStream in=Propertie
sDemo.class.getResourceAsStream("/db.properties");
何为sourcefolder
代码不将其当做文件来处理,程序员用来做文件归类所用
将资源文件存放在web-inf下
package com.zking.pres; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ParseServlet extends HttpServlet{ private static final long serialVersionUID = -1102190634374988166L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doPost(req, resp); } @Override 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")); } }
配置Xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Xmlpres</display-name> <servlet> <servlet-name>ParseServlet</servlet-name> <servlet-class>com.zking.pres.ParseServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ParseServlet</servlet-name> <url-pattern>/ParseServlet</url-pattern> </servlet-mapping> </web-app>
解析指定路径下的资源文件(全部代码)
package com.zking.pres; import java.io.InputStream; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader; /* * dom4j解析 * jdom jdk sax解析 * 解析指定路径下的资源文件 * * */ public class XmlDemo { public static void main(String[] args) throws Exception { InputStream in= XmlDemo.class.getResourceAsStream("config.xml"); SAXReader saxReader=new SAXReader(); Document doc=saxReader.read(in); System.out.println(doc.asXML()); //xpath解析 能够将XML格式的串当做目录结构来进行查找 List<Element> stuEles= doc.selectNodes("/students/student"); for (Element stuEle : stuEles) { if ("s002".equals(stuEle.attributeValue("sid"))) { System.out.println(stuEle.asXML()); Element nameELe=(Element) stuEle.selectSingleNode("name"); System.out.println(nameELe.asXML()); System.out.println(nameELe.getText()); } }
简单代码解析
Element stuEle= (Element) doc.selectSingleNode("/students/student[@sid='s002']");
System.out.println(stuEle.asXML());