XML的介绍
1.XML作用
1.1配置 (保存数据)
*.xml和*.properties INI yml springboot
1.2数据交互(获取第三方数据)
webservice xml ajax json
2.Java中3种配置位置及读取方式
2.1如何使用Properties读取配置文件
1)*.properties文件以键值对的方式存储数据;
2)使用Properties类读取配置文件;
2.2 配置位置
1)存放于根目录下,/代表获取src根目录的绝对路径
2)存放于同一类的包下,不加/代表同类名包下的相对路径;
3)存放于WEB-INF目录下
ServletContext sc=this.getServletContext();
InputStream is=sc.getResourceAsStream("/WEB-INF/db.properties");
3.dom4j+xpath解析xml文件 document for java SAX
1)xpath类似数据库中的select语句;
2)Document有节点(Node)组成:元素.节点、属性、文本等
3) Node(节点) Element(元素) 区别 getText();
3)selectNodes()获取节点下所有子节点;
4)selectSingleNode()获取单个节点信息;
5) xpath语法:/(定位路径)、@(获取属性)
4.什么叫XML建模
将XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模
5. XML建模
1)根据XML配置文件元素节点创建元素节点实体类
ConfigModel、ActionModel、ForwardModel
2)利用dom4j+xpath技术实现XML建模
ConfigModelFactory
建模的思维导图
解析的代码演示
package demo;
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 Demo03 {
public static void main(final String[] args) {
// 1.获取io流
final InputStream is = Demo03.class.getResourceAsStream("/students.xml");
//System.out.println(is);
//2.创建SaxReader对象 读取器对象
final SAXReader sr = new SAXReader();
try {
//3.读取配置文件,获取Docement对象
Document doc = sr.read(is);
//4.使用xpath解析docement对象
/*Node node=doc.selectSingleNode("/students/student[@sid='s001']");
System.out.println(node);
Element el=(Element)node;
String value=el.attributeValue("sid");
System.out.println(value);*/
//List nodes= doc.selectNodes("//*[contains(@sid,'00')]");
//List nodes= doc.selectNodes("//*[starts-with(@sid,'1')]");
List nodes= doc.selectNodes("//*[ends-with(@sid,'1')]");
System.out.println(nodes);
} catch (final DocumentException e) {
throw new RuntimeException(e);
}
}
}
建模的代码演示
package com.zking.xml.entity;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**xml建模 ActionModel对象 对应config.xml文件中Action标签
* @author zjjt
*
*/
public class ActionModel implements Serializable {
private static final long serialVersionUID = 1L;
private String path;
private String type;
//Action标签下的forward标签对应的对象集合,以forward中name属性为键,该对象为值
private Map<String, ForwardModel> forwardModels=
new HashMap<String, ForwardModel>();
public ActionModel() {
super();
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Map<String, ForwardModel> getForwardModels() {
return forwardModels;
}
public void setForwardModels(Map<String, ForwardModel> forwardModels) {
this.forwardModels = forwardModels;
}
@Override
public String toString() {
return "ActionModel [path=" + path + ", type=" + type + ", forwardModels=" + forwardModels + "]";
}
/**
* 将forwardMode对象放入到ActionMode的map集合中
*/
public void put(ForwardModel forwardModel) {
this.forwardModels.put(forwardModel.getName(), forwardModel);
}
/**从map集合中通过forwardModel的name属性值获取forwardModel对象
* @param forwardModel
* @return
*/
public ForwardModel get(String name) {
return this.forwardModels.get(name);
}
}
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;
import com.zking.xml.entity.ActionModel;
import com.zking.xml.entity.ConfigModel;
import com.zking.xml.entity.ForwardModel;
public class ConfiModelFactory {
// 配置文件默认路径
public static final String DEFAUL_PATH = "/config.xml";
// 实例方法 对象名.test()
public void test() {
}
//私有构造方法 外部不能实例化对象
private ConfiModelFactory() {
}
/*
* public ConfiModelFactory() { super(); // TODO Auto-generated constructor stub
* }
*/
//静态方法 类名.createConfiModel()
//建模的方法
public static ConfigModel createConfiModel(String path) {
// 1.获取io流
InputStream is = ConfiModelFactory.class.getResourceAsStream(path);
// 定义变量
ActionModel am = null;
String actionPath = null;
String actionType = null;
ForwardModel fm = null;
String forwardName = null;
String forwardPath = null;
String forwardRedirect = null;
ConfigModel configModel = new ConfigModel();
try {
// 2.创建读取器对象 SAxReader
SAXReader sr = new SAXReader();
// 3.读取配置文件,生成document对象
Document doc = sr.read(is);
// 4.开始解析
// 4.1获取Config标签下的Action标签节点
List<Node> actionNodes = doc.selectNodes("/config/action");
// 4.2遍历Action标签,获取action中path和type值
for (Node actionNode : actionNodes) {
// 将node对象转换成Element对象
Element actionEl = (Element) actionNode;
// 实例化ActionModel对象
am = new ActionModel();
// 获取action中的path和type属性值
actionPath = actionEl.attributeValue("path");
actionType = actionEl.attributeValue("type");
// 将获取的path和type属性值设置到ActionModel对象中
am.setPath(actionPath);
am.setType(actionType);
// 获取该action下的forward标签
List<Node> forwardNodes = actionNode.selectNodes("forward");
// 遍历forward标签,获取属性值
for (Node forwardNode : forwardNodes) {
// 将node对象转换成Element对象
Element forwardEl = (Element)forwardNode;
forwardName = forwardEl.attributeValue("name");
forwardPath = forwardEl.attributeValue("path");
forwardRedirect = forwardEl.attributeValue("redirect");
// 实例化ForwardModel对象
fm = new ForwardModel();
// 将属性值设置到对象中
fm.setName(forwardName);
fm.setPath(forwardPath);
fm.setRedirect(Boolean.parseBoolean(forwardRedirect));
// 将ForwardModel对象放入到ActionModel对象的集合中
am.put(fm);
}
// 将ActionModel对象放入到configMode对象的map集合中
configModel.put(am);
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return configModel;
}
public static ConfigModel createConfiModel() {
return createConfiModel(DEFAUL_PATH);
}
public static void main(String[] args) {
ConfigModel createConfiModel = ConfiModelFactory.createConfiModel();
ActionModel actionModel = createConfiModel.get("/regAction");
System.out.println(actionModel.getType());
System.out.println(actionModel.getPath());
ForwardModel forwardModel = actionModel.get("success");
System.out.println(forwardModel.getPath());
System.out.println(forwardModel.isRedirect());
}
}