1.XML作用
1.1配置
*.xml和*.properties、*.ini、*.yaml
1.2数据交互(获取第三方数据)
XML:webservices(axis2) -> xml -> 手机归属地、天气
JSON:ajax 无刷新
2.Java中3种配置位置及读取方式
2.1如何使用Properties读取配置文件
1)*.properties文件以键值对的方式存储数据;
username=\u91D1\u6CF0\u4EA8
password=777
url=www.4399.com
2)使用Properties类读取配置文件;
package com.zking.xml.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* Properties读取配置文件
*
* @author Administrator
*
*/
public class DemoProperties {
public static void main(String[] args) throws IOException {
// 获取文件输入流
InputStream is = DemoProperties.class.getResourceAsStream("Demo.properties");
// 创建对象
Properties pt = new Properties();
// 读取文件输入流
pt.load(is);
// 读取配置
String username = pt.getProperty("username");
String password = pt.getProperty("password");
Object object = pt.getOrDefault("url", "http://www.777.com");
// 打印输出
System.out.println(username);
System.out.println(password);
System.out.println(object);
}
}
2.2 配置位置
1)存放于根目录下,/代表获取src根目录的绝对路径
// 1)存放于根目录下,/代表获取src根目录的绝对路径
InputStream is = DemoURL.class.getResourceAsStream("/Student.xml");
System.out.println(is);
2)存放于同一类的包下,不加/代表同类名包下的相对路径;
// 2)存放于同一类的包下,不加/代表同类名包下的相对路径;
InputStream stream = DemoURL.class.getResourceAsStream("config.xml");
System.out.println(stream);
3)存放于WEB-INF目录下
package com.zking.xml.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/URL")
public class URLServlet extends HttpServlet {
// 读取方式:
// 因为是存放在web-inf目录下,情况比较特殊,不能被外界直接访问,所以只能通过间接的方式去拿到值。
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取文件输入流
InputStream is = req.getServletContext().getResourceAsStream("/WEB-INF/WI.properties");
// 创建对象
Properties p = new Properties();
// 读取文件输入流
p.load(is);
// 打印输出
System.out.println(p.getProperty("url"));
System.out.println(p.getProperty("context"));
}
}
3.dom4j+xpath解析xml文件
1)xpath类似数据库中的select语句;
2)Document有节点(Node)组成:元素节点、属性、文本等;
3)selectNodes()获取节点下所有子节点;
4)selectSingleNodes()获取单个节点信息;
5) xpath语法:/(定位路径)、@(获取属性)
package com.zking.xml.utils;
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;
/**
* XML读取配置文件(dom4j+xpath) 获取Student.xml
*
* @author Administrator
*
*/
public class DemoXML {
public static void main(String[] args) {
try {
// 获取Student.xml的文件输入流
InputStream is = DemoXML.class.getResourceAsStream("/Student.xml");
// 创建SAXReader
SAXReader s = new SAXReader();
// 读取文件输入流并转换成document
// Document:文档对象,包含文本、属性和对象
Document document = s.read(is);
// 循环读取
// selectSingleNode:获取单个节点
// selectNodes:获取多个节点
List<Node> nodes = document.selectNodes("/student/stu");
for (Node node : nodes) {
// 将Node节点转换成元素节点
Element e = (Element) node;
// 获取节点中的属性
String sid = e.attributeValue("sid");
System.out.println("sid=" + sid);
String sex = e.attributeValue("sex");
System.out.println("sex=" + sex);
// 获取Student节点下的多个节点
Node name = e.selectSingleNode("name");
String text = name.getText();
System.out.println(text);
}
// 获取Student.xml中<Student sid="siid">中的mail属性的文本
// (定位路径)、@(获取属性)
String text = document.selectSingleNode("/student/stu[@sid='siid']/contact/mail").getText();
System.out.println(text);
} catch (Exception e) {
e.printStackTrace();
}
}
}
拓展:
读取config.xml
package com.zking.xml.utils;
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;
/**
* XML读取配置文件(dom4j+xpath) 获取config.xml文件
*
* @author Administrator
*
*/
public class DemoXMLWork {
public static void main(String[] args) {
try {
// 获取config.xml文件
InputStream is = DemoXMLWork.class.getResourceAsStream("/config.xml");
// 创建SAXReader
SAXReader sr = new SAXReader();
// 转换成Document
Document document = sr.read(is);
// 循环读取
List<Node> nodes = document.selectNodes("/config/action");
for (Node node : nodes) {
Element el = (Element) node;
// 获取节点属性
String path = el.attributeValue("path");
String type = el.attributeValue("type");
System.out.println("path=" + path);
System.out.println("type=" + type);
List<Node> nodess = document.selectNodes("/config/action/forward");
for (Node node2 : nodess) {
Element elm = (Element) node2;
String name = elm.attributeValue("name");
String path2 = elm.attributeValue("path");
String redirect = elm.attributeValue("redirect");
System.out.println(name);
System.out.println(path2);
System.out.println(redirect);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}