一、XML的解析
①存放位置:
1. src根目录下
xxx.class.getResourceAsStream("/xxx.xml");
2. 与读取配置文件的类在同一个包
xxx.class.getResourceAsStream(“xxx.xml”);
3. WEB-INF(或其子目录下)
ServletContext application = this.getServletContext();
InputStream is = application.getResourceAsStream("/WEB-INF/xxx.xml");
②解析方式
这里采用的是dom4j+xpath的方式(dom4j网上有jar包):
document.selectNodes(xpath);//查一组
document.selectSingleNode(xpath);//查单个
二、XML的建模
简单来说,建模就是把XML解析出来的数据封装为Java中的类,这样在一个项目中只需要解析一次,便可以在项目的任意地方调用数据。下面是项目的结构:
首先是XML文件:
config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config[
<!ELEMENT config (action*)>
<!ELEMENT action (forward*)>
<!ELEMENT forward EMPTY>
<!ATTLIST action
path CDATA #REQUIRED
type CDATA #REQUIRED
>
<!ATTLIST forward
name CDATA #REQUIRED
path CDATA #REQUIRED
redirect (true|false) "false"
>
]>
<!-- config标签:可以包含0~N个action标签 -->
<config>
<!-- action标签:可以饱含0~N个forward标签 path:以/开头的字符串,并且值必须唯一 type:字符串 -->
<action path="/regAction" type="test.RegAction">
<!-- forward标签:没有子标签; name:字符串,同一action标签下的forward标签name值不能相同 ; path:以/开头的字符串
redirect:只能是false|true,允许空,默认值为false -->
<forward name="failed" path="/reg.jsp" redirect="false" />
<forward name="success" path="/login.jsp" redirect="true" />
</action>
<action path="/loginAction" type="test.LoginAction">
<forward name="failed" path="/login.jsp" redirect="false" />
<forward name="success" path="/main.jsp" redirect="true" />
</action>
</config>
然后是模型(实体类):
创建依据:
1.没有子标签
2.它属于最后一个标签
该模型需要包含哪些内容:
1.所拥有的属性
2.所拥有的子标签
①ForwardModel
package demo;
public class ForwardModel {
private String name;
private String path;
private boolean redirect = false;
public ForwardModel() {}
public ForwardModel(String name, String path, boolean redirect) {
this.name = name;
this.path = path;
this.redirect = redirect;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
Factory.checkPath(path);
this.path = path;
}
public boolean isRedirect() {
return redirect;
}
public void setRedirect(String redirect) {
this.redirect = Boolean.parseBoolean(redirect);
}
public void setRedirect(boolean redirect) {
this.redirect = redirect;
}
@Override
public String toString() {
return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
}
②ActionModel
package demo;
import java.util.HashMap;
import java.util.Map;
public class ActionModel {
private String path;
private String type;
private Map<String, ForwardModel> m = new HashMap<String, ForwardModel>();
public ActionModel() {}
public ActionModel(String path, String type, Map<String, ForwardModel> m) {
this.path = path;
this.type = type;
this.m = m;
}
public String getPath() {
return path;
}
public void setPath(String path) {
Factory.checkPath(path);
this.path = path;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Map<String, ForwardModel> getM() {
return m;
}
@Override
public String toString() {
return "ActionModel [path=" + path + ", type=" + type + ", m=" + m + "]";
}
public void put(ForwardModel fm) {
if(null == fm) {
throw new RuntimeException("ForwardModel为空");
}
else if(this.m.containsKey(fm.getName())) {
throw new RuntimeException(fm.getName()+"已存在");
}
this.m.put(fm.getName(), fm);
}
public ForwardModel find(String name) {
if(!this.m.containsKey(name)) {
throw new RuntimeException(name+"不存在");
}
return this.m.get(name);
}
}
③ConfigModel
package demo;
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
private static final long serialVersionUID = 1l;
private static ConfigModel configmodel;
private Map<String, ActionModel> m = new HashMap<String, ActionModel>();
public ConfigModel(String pd) {
if(!"bao".equals(pd)) {
throw new RuntimeException("您无权创建此对象的实例");
}
}
public Map<String, ActionModel> getM() {
return m;
}
public void put(ActionModel am) {
if(null == am) {
throw new RuntimeException("ActionModel为空");
}
else if(this.m.containsKey(am.getPath())) {
throw new RuntimeException(am.getPath()+"已存在");
}
this.m.put(am.getPath(), am);
}
public ActionModel find(String path) {
if(!this.m.containsKey(path)) {
throw new RuntimeException(path+"不存在");
}
return this.m.get(path);
}
}
这里,我又创建了一个工厂类:
Factory
package demo;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Factory {
private static final String PATH="/config.xml";
private static final Pattern PATTERN = Pattern.compile("^/.+$");
private static ConfigModel cm;
private Factory() {}
public static ConfigModel createConfigModel(String path) throws IOException, DocumentException {
if(null == cm) {
cm = new ConfigModel("bao");
init(path);
}
return cm;
}
private static void init(String initPath) throws IOException, DocumentException {
InputStream is = null;
if(null == initPath) {
is = Factory.class.getResourceAsStream(PATH);
}
else {
is = Factory.class.getResourceAsStream(initPath);
}
SAXReader saxr = new SAXReader();
Document d = saxr.read(is);
is.close();
List<Element> l = d.selectNodes("/config/action");
for (Element e : l) {
String path = e.attributeValue("path");
String type = e.attributeValue("type");
ActionModel am = new ActionModel();
am.setPath(path);
am.setType(type);
cm.put(am);
List<Element> lt = e.selectNodes("forward");
for (Element el : lt) {
String name = el.attributeValue("name");
path = el.attributeValue("path");
String redirect = el.attributeValue("redirect");
ForwardModel fm = new ForwardModel();
fm.setName(name);
fm.setPath(path);
fm.setRedirect(redirect);
am.put(fm);
}
}
}
public static void checkPath(String path) {
Matcher m = PATTERN.matcher(path);
if(!m.matches()) {
throw new RuntimeException(path+"没有/");
}
}
}
最后,测试一下效果,代码如下:
Demo
package demo;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import org.dom4j.DocumentException;
public class Demo {
public static void main(String[] args) throws IOException, DocumentException {
// ConfigModel cm = new ConfigModel(null);
// ConfigModel cm = Factory.createConfigModel("config.xml");
ConfigModel cm = Factory.createConfigModel(null);
Map<String, ActionModel> m = cm.getM();
Set<String> s = m.keySet();
for (String string : s) {
System.out.println("ActionModel:"+m.get(string));
}
ActionModel am = cm.find("/loginAction");
ForwardModel fm = am.find("success");
System.out.println("原先存在:"+fm);
fm = new ForwardModel();
fm.setName("sa");
fm.setPath("/sert");
fm.setRedirect("true");
am.put(fm);
System.out.println("后来添加:"+fm);
}
}
运行效果:
好了,今天的分享就到这,希望我们以后共同进步,谢谢大家!