思维导图:
XML建模就是以面向对象的编程思想,将指定的xml字符串当作对象来操作
需要导入的包:
Ⅰ:dom4j-1.6.1.jar
Ⅱ:jaxen-1.1-beta-6.jar
xml的文件的结构(导图):
先是forword
package com.LSZ;
public class FowardModel {
private String name;
private String path;
private String redirect;
public FowardModel() {
// TODO Auto-generated constructor stub
}
public FowardModel(String name, String path, String redirect) {
super();
this.name = name;
this.path = path;
this.redirect = redirect;
}
@Override
public String toString() {
return "FowardModel [name=" + name + ", path=" + path + ", 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) {
this.path = path;
}
public String getRedirect() {
return redirect;
}
public void setRedirect(String redirect) {
this.redirect = redirect;
}
}
actionmodel
package com.LSZ;
import java.util.HashMap;
import java.util.Map;
public class ActionModel {
private String path;
private String type;
private Map<String, FowardModel> map=new HashMap<String, FowardModel>();
public ActionModel() {
// TODO Auto-generated constructor stub
}
public ActionModel(String path, String type) {
super();
this.path = path;
this.type = type;
}
@Override
public String toString() {
return "ActionModel [path=" + path + ", type=" + type + "]";
}
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 void push(FowardModel a) {
map.put(a.getPath(), a);
}
public FowardModel pop(String name) {
return map.get(name);
}
}
configmodel
package com.LSZ;
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
Map<String, ActionModel> map=new HashMap<>();
public void push(ActionModel a) {
map.put(a.getPath(), a);
}
public ActionModel pop(String path) {
return map.get(path);
}
}
建模
package com.LSZ;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ConfigModelFactory {
public ConfigModel buibl() throws Exception {
return buibl("config.xml");
}
private ConfigModel buibl(String paths) throws Exception {
InputStream in = ConfigModelFactory.class.getResourceAsStream(paths);
// 得到解析器
SAXReader sr = new SAXReader();
Document doc = sr.read(in);
ConfigModel con = new ConfigModel();
// 拿到对应的节点对象
List<Element> action = doc.selectNodes("/config/action");
for (Element element : action) {
ActionModel a = new ActionModel();
a.setPath(element.attributeValue("path"));
a.setType(element.attributeValue("type"));
con.push(a);
List<Element> forward = element.selectNodes("forward");
for (Element element2 : forward) {
FowardModel f = new FowardModel();
f.setName(element2.attributeValue("name"));
f.setPath(element2.attributeValue("path"));
f.setRedirect(element2.attributeValue("redirect"));
a.push(f);
}
}
return con;
}
}
测试类
package com.LSZ;
public class test {
public static void main(String[] args) throws Exception {
ConfigModel c = new ConfigModelFactory().buibl();
ActionModel pop = c.pop("/loginAction");
System.out.println(pop.getType());
}
}
输出结果
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>jrebelServlet</servlet-name>
<servlet-class>com.zking.xml.JrebelServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jrebelServlet</servlet-name>
<url-pattern>/jrebelServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>jrebelServlet2</servlet-name>
<servlet-class>com.zking.xml.JrebelServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jrebelServlet2</servlet-name>
<url-pattern>/jrebelServlet2</url-pattern>
<url-pattern>/jrebelServlet3</url-pattern>
</servlet-mapping>
</web-app>
1.ServletModel类
package com.LSZ;
public class ServletModel extends WebAppModel{
private String servletname;
private String servletcalss;
public String getServletname() {
return servletname;
}
public void setServletname(String servletname) {
this.servletname = servletname;
}
public String getServletcalss() {
return servletcalss;
}
public void setServletcalss(String servletcalss) {
this.servletcalss = servletcalss;
}
public ServletModel(String servletname, String servletcalss) {
super();
this.servletname = servletname;
this.servletcalss = servletcalss;
}
@Override
public String toString() {
return "ServletDodel [servletname=" + servletname + ", servletcalss=" + servletcalss + "]";
}
public ServletModel() {
// TODO Auto-generated constructor stub
}
}
2.ServletMappingModel 类
package com.LSZ;
public class ServletMappingModel extends WebAppModel{
private String servletname;
private String urlpattern;
public String getServletname() {
return servletname;
}
public void setServletname(String servletname) {
this.servletname = servletname;
}
public String getUrlpattern() {
return urlpattern;
}
public void setUrlpattern(String urlpattern) {
this.urlpattern = urlpattern;
}
@Override
public String toString() {
return "ServletMappingDodel [servletname=" + servletname + ", urlpattern=" + urlpattern + "]";
}
public ServletMappingModel(String servletname, String urlpattern) {
super();
this.servletname = servletname;
this.urlpattern = urlpattern;
}
public ServletMappingModel() {
// TODO Auto-generated constructor stub
}
}
3:WebappModel 类
package com.LSZ;
import java.util.HashMap;
import java.util.Map;
public class WebAppModel {
Map<String, WebAppModel> map = new HashMap<>();
public void push(WebAppModel w) {
if (w instanceof ServletModel) {// 判断w是不是ServletDodel的实类
map.put(((ServletModel) w).getServletname(), (ServletModel) w);
}
if (w instanceof ServletMappingModel) {// 判断w是不是ServletMappingDodel的实类
map.put(((ServletMappingModel) w).getUrlpattern(), (ServletMappingModel) w);
}
}
public WebAppModel pop(String name) {
return map.get(name);
}
}
4:建模
package com.LSZ;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class WebAppModelFactory {
public WebAppModel biblu() throws DocumentException {
return biblu("web.xml");
}
private WebAppModel biblu(String path) throws DocumentException {
// 得到文件输入流
InputStream in = ServletMappingModel.class.getResourceAsStream(path);
// 解析器
SAXReader sr = new SAXReader();
Document dom = sr.read(in);
// 定义WebappModel 容器
WebAppModel w = new WebAppModel();
List<Element> servletent = dom.selectNodes("/web-app/servlet");
for (Element element : servletent) {
// 赋值
ServletModel ser = new ServletModel();
ser.setServletcalss(element.selectSingleNode("servlet-class").getText());
ser.setServletname(element.selectSingleNode("servlet-name").getText());
w.push(ser);
}
List<Element> servletmapping = dom.selectNodes("/web-app/servlet-mapping");
for (Element element : servletmapping) {
// 赋值
ServletMappingModel sermin = new ServletMappingModel();
sermin.setServletname(element.selectSingleNode("servlet-name").getText());
sermin.setUrlpattern(element.selectSingleNode("url-pattern").getText());
w.push(sermin);
}
return w;
}
}
5:测试类
package com.LSZ;
import org.dom4j.DocumentException;
public class TRY {
public static void main(String[] args) throws DocumentException {
WebAppModel w = new WebAppModelFactory().biblu();
/**
* 拿到ServletMappingDodel 类 再通过ServletMappingDodel类 的 servletname属性
* 拿ServletDodel类的servletcalss 属性
*/
ServletMappingModel pop = (ServletMappingModel) w.pop("/jrebelServlet2");
ServletModel pop2 = (ServletModel) w.pop(pop.getServletname());
System.out.println(pop2.getServletcalss());
}
}
运行结果