Xml解析作业与Xml建模andXml建模作业
作业:config.xml解析
1、获取所有action中的type的值
public static void main(String[] args) throws Exception {
InputStream in=XmlDemo.class.getResourceAsStream("config.xml");
SAXReader sax= new SAXReader();
Document doc=sax.read(in);
//获取所有action中的type的值
List<Element> stuEles= doc.selectNodes("/config/action");
for (Element stuEle : stuEles) {
String type=stuEle.attributeValue("type");
System.out.println(type); }
2、获取第二个action中的type的值
List<Element> stuEles= doc.selectNodes("/config/action");
for (Element stuEle : stuEles) {
if("/loginAction".equals(stuEle.attributeValue("path"))) {
String type=stuEle.attributeValue("type");
System.out.println(type);
}
}
3、获取第二个action的所有forward的path
List<Element> stuEles= doc.selectNodes("/config/action");
for (Element stuEle : stuEles) {
if("/loginAction".equals(stuEle.attributeValue("path"))) {
List<Element> ford=(List<Element>) stuEle.selectNodes("forward");
for (Element element : ford) {
String path=element.attributeValue("path");
System.out.println(path);
}
}
}
4、获取第二个action的第二个forward的path
List<Element> stuEles= doc.selectNodes("/config/action");
for (Element stuEle : stuEles) {
if("/loginAction".equals(stuEle.attributeValue("path"))) {
List<Element> ford=(List<Element>) stuEle.selectNodes("forward");
for (Element element : ford) {
if("success".equals(element.attributeValue("name"))) {
String path=element.attributeValue("path");
System.out.println(path);
}
} }
}
Xml建模
1.什么是Xml建模
将XML配置文件中的元素,属性,文本信息 转换成对象的过程叫XML建模
2.XML建模
1.根据XML配置文件元素节点创建元素,节点,实体类
ConfigModel ActionModel ForwardModel
2.利用dom4j+xpath技术实现XML建模ConfigModelFactory
2. XML建模类
ConfigModel
ActionModel
ForwardModel
ConfigModelFactory
建模的思路
1、分析需要被建模的文件中有那几个对象
2、每个对象拥有的行为以及属性
3、定义对象从小到大(从里到外)
4、通过23种的设计模式中的工厂模式,解析xml生产出指定对象
好处:
提高代码的复用性
4.利用工程模式+dom4j+xpath解析XML配置文件
Config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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>
3.使用Map集合存放子节点元素,其中Key为子节点的唯一属性,Value为整个子节点对象。
定义对象实体类
package com.zking.model; public class FowardModel {
/*
* 添加节点属性
*/
private String name;
private String path;
private Boolean reairect;
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 Boolean getReairect() {
return reairect;
}
public void setReairect(Boolean reairect) {
this.reairect = reairect;
}
}
行为及属性
package com.zking.model; import java.util.HashMap;
import java.util.Map; public class ActionModel { private String path;
private String type;
private Map<String, FowardModel> fmap=new HashMap<>();
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;
} //存放到Map集合
public void push(FowardModel fowardModel) {
fmap.put(fowardModel.getName(), fowardModel);
}
//获取 返回FowardModel
public FowardModel pop(String name) {
return fmap.get(name); }
}
package com.zking.model; import java.util.HashMap;
import java.util.Map; public class ConfigModel { private Map<String, ActionModel> amap=new HashMap<>();
public void push(ActionModel actionModel) {
amap.put(actionModel.getPath(), actionModel);
}
public ActionModel pop(String path) {
return amap.get(path);
} }
4.利用工程模式+dom4j+xpath解析XML配置文件
导入dom4j包
package com.zking.model; 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; /*
*
* 23设计模式之一:工厂模式 用来将资源文件生产指定的实体类
* 处理Java中所遇到的一些特定的一些问题
*
* 好处:
* 提高代码复用性
*/
public class ConfigModelFactory { public static ConfigModel build() throws DocumentException {
return ConfigModel("config.xml");
}
/*
* 生产出有类容的实体类ConfigModel
*/
private static ConfigModel ConfigModel(String xmlPath) throws DocumentException {
// TODO Auto-generated method stub
// 创建ConfigModel对象
ConfigModel configModel=new ConfigModel();
ActionModel actionModel=null;
FowardModel fowardModel=null;
InputStream in = ConfigModelFactory.class.getResourceAsStream(xmlPath);
SAXReader saxRead=new SAXReader();
Document doc = saxRead.read(in); List<Element> actionEles = doc.selectNodes("/config/action");
for (Element actionEle : actionEles) {
actionModel=new ActionModel(); //给actionModel对象填充Xml中的action标签的类容
actionModel.setPath(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type")); List<Element> fowardEles = actionEle.selectNodes("foward");
for (Element fowardEle : fowardEles) {
fowardModel=new FowardModel(); //给fowardModel对象填充Xml中的foward标签的类容
fowardModel.setName(fowardEle.attributeValue("name"));
fowardModel.setPath(fowardEle.attributeValue("path"));
fowardModel.setReairect(!"false".equals(fowardEle.attributeValue("redirect")));
//<forward name="failed" path="/login.jsp" redirect="false" />
// redirect默认true 重定向
//只有false才是转发
//fowardEle.attributeValue("redirect")拿到是xml中你所填的值
//不填 重定向 !"false".equals(fowardEle.attributeValue("redirect")) false
//填 true 重定向 !"false".equals(fowardEle.attributeValue("redirect")) false
//填false 转发 !"false".equals(fowardEle.attributeValue("redirect")) true actionModel.push(fowardModel);
} configModel.push(actionModel);
}
return configModel;
}
public static void main(String[] args) throws DocumentException {
ConfigModel configModel=ConfigModelFactory.build();
ActionModel actionModel= configModel.pop("/loginAction");
System.out.println(actionModel.getType());
} }
控制台输入结果:
Xml建模作业
ServletClassModel
package com.zking.xml.webModel; public class ServletClassModel {
private String context; public String getContext() {
return context;
} public void setContext(String context) {
this.context = context;
}
}
ServletMappingModel
package com.zking.xml.webModel; import java.util.ArrayList;
import java.util.List; public class ServletMappingModel {
private ServletNameModel servletNameModel;
private List<UrlPatternModel> urlPatternModels = new ArrayList<>();
public ServletNameModel getServletNameModel() {
return servletNameModel;
}
public void setServletNameModel(ServletNameModel servletNameModel) {
this.servletNameModel = servletNameModel;
} public void pushUrlPatternModel(UrlPatternModel urlPatternModel) {
urlPatternModels.add(urlPatternModel);
} public List<UrlPatternModel> getUrlPatternModels() {
return urlPatternModels;
}
}
ServletModel
package com.zking.xml.webModel; public class ServletModel {
private ServletNameModel servletNameModel;
private ServletClassModel servletClassModel; public ServletNameModel getServletNameModel() {
return servletNameModel;
} public void setServletNameModel(ServletNameModel servletNameModel) {
this.servletNameModel = servletNameModel;
} public ServletClassModel getServletClassModel() {
return servletClassModel;
} public void setServletClassModel(ServletClassModel servletClassModel) {
this.servletClassModel = servletClassModel;
} }
ServletNameModel
package com.zking.xml.webModel; public class ServletNameModel {
private String context; public String getContext() {
return context;
} public void setContext(String context) {
this.context = context;
}
}
UrlPatternModel
package com.zking.xml.webModel; public class UrlPatternModel {
private String context; public String getContext() {
return context;
} public void setContext(String context) {
this.context = context;
} }
WebAppModel
package com.zking.xml.webModel; import java.util.ArrayList;
import java.util.List; public class WebAppModel {
private List<ServletModel> servletModels = new ArrayList<>();
private List<ServletMappingModel> servletMappingModels = new ArrayList<>(); public void pushServletModel(ServletModel servletModel) {
servletModels.add(servletModel);
} public List<ServletModel> getServletModels() {
return servletModels;
} public void pushServletMappingModel(ServletMappingModel servletMappingModel) {
servletMappingModels.add(servletMappingModel);
} public List<ServletMappingModel> getServletMappingModels() {
return servletMappingModels;
} }
WebAppModelFactory
package com.zking.xml.webModel; 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 WebAppModelFactory {
public static WebAppModel buildWebAppModel() {
String xmlPath = "/web.xml";
return buildWebAppModel(xmlPath);
} public static WebAppModel buildWebAppModel(String xmlPath) {
InputStream in = WebAppModelFactory.class.getResourceAsStream(xmlPath);
SAXReader saxReader = new SAXReader();
WebAppModel webAppModel = new WebAppModel();
try {
Document doc = saxReader.read(in); List<Element> servletEles = doc.selectNodes("/web-app/servlet");
for (Element servletEle : servletEles) {
ServletModel servletModel = new ServletModel(); Element servletNameEle = (Element) servletEle.selectSingleNode("servlet-name");
Element servletClassEle = (Element) servletEle.selectSingleNode("servlet-class");
ServletNameModel servletNameModel = new ServletNameModel();
ServletClassModel servletClassModel = new ServletClassModel();
servletNameModel.setContext(servletNameEle.getText());
servletClassModel.setContext(servletClassEle.getText()); servletModel.setServletNameModel(servletNameModel);
servletModel.setServletClassModel(servletClassModel); webAppModel.pushServletModel(servletModel);
} List<Element> servletMappingEles = doc.selectNodes("/web-app/servlet-mapping");
for (Element servletMappingEle : servletMappingEles) {
ServletMappingModel servletMappingModel = new ServletMappingModel(); Element servletNameEle = (Element) servletMappingEle.selectSingleNode("servlet-name");
ServletNameModel servletNameModel = new ServletNameModel();
servletNameModel.setContext(servletNameEle.getText());
servletMappingModel.setServletNameModel(servletNameModel); List<Element> urlPatternEles = servletMappingEle.selectNodes("url-pattern");
for (Element urlPatternEle : urlPatternEles) {
UrlPatternModel urlPatternModel = new UrlPatternModel();
urlPatternModel.setContext(urlPatternEle.getText());
servletMappingModel.pushUrlPatternModel(urlPatternModel);
} webAppModel.pushServletMappingModel(servletMappingModel);
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return webAppModel;
} public static String getServletClassByUrl(WebAppModel webAppModel, String url) {
String servletClass = ""; String servletName = "";
List<ServletMappingModel> servletMappingModels = webAppModel.getServletMappingModels();
for (ServletMappingModel servletMappingModel : servletMappingModels) {
List<UrlPatternModel> urlPatternModels = servletMappingModel.getUrlPatternModels();
for (UrlPatternModel urlPatternModel : urlPatternModels) {
if(url.equals(urlPatternModel.getContext())) {
ServletNameModel servletNameModel = servletMappingModel.getServletNameModel();
servletName = servletNameModel.getContext();
}
}
} List<ServletModel> servletModels = webAppModel.getServletModels();
for (ServletModel servletModel : servletModels) {
ServletNameModel servletNameModel = servletModel.getServletNameModel();
if(servletName.equals(servletNameModel.getContext())) {
ServletClassModel servletClassModel = servletModel.getServletClassModel();
servletClass = servletClassModel.getContext();
}
}
return servletClass;
} public static void main(String[] args) {
WebAppModel webAppModel = WebAppModelFactory.buildWebAppModel();
String res = getServletClassByUrl(webAppModel, "/jrebelServlet");
String res2 = getServletClassByUrl(webAppModel, "/jrebelServlet2");
String res3 = getServletClassByUrl(webAppModel, "/jrebelServlet3");
System.out.println(res);
System.out.println(res2);
System.out.println(res3); }
}
2019-06-1119:22:01
Xml解析作业与Xml建模andXml建模作业的更多相关文章
- XML解析——Java中XML的四种解析方式
XML是一种通用的数据交换格式,它的平台无关性.语言无关性.系统无关性.给数据集成与交互带来了极大的方便.XML在不同的语言环境中解析方式都是一样的,只不过实现的语法不同而已. XML的解析方式分为四 ...
- XML解析——Java中XML的四种解析方式(转载 by 龍清扬)
XML是一种通用的数据交换格式,它的平台无关性.语言无关性.系统无关性.给数据集成与交互带来了极大的方便.XML在不同的语言环境中解析方式都是一样的,只不过实现的语法不同而已. XML的解析方式分为四 ...
- 【Python】 xml解析与生成 xml
xml *之前用的时候也没想到..其实用BeautifulSoup就可以解析xml啊..因为html只是xml的一种实现方式吧.但是很蛋疼的一点就是,bs不提供获取对象的方法,其find大多获取的都是 ...
- XML解析技术研究(一)
摘要:XML作为过去十年中出现的最流行的技术之一,得到了广泛的应用,而其中XML解析技术是XML应用的关键.本文介绍了XML解析技术的研究动向,分析和比较了4种XML解析技术的优劣,并归纳总结了应 ...
- Python XML解析(转载)
Python XML解析 什么是XML? XML 指可扩展标记语言(eXtensible Markup Language). 你可以通过本站学习XML教程 XML 被设计用来传输和存储数据. XML是 ...
- JAVA基础学习之XMLCDATA区、XML处理指令、XML约束概述、JavaBean、XML解析(8)
1.CDATA区在编写XML文件时,有些内容可能不想让解析引擎解析执行,而是当作原始内容处理.遇到此种情况,可以把这些内容放在CDATA区里,对于CDATA区域内的内容,XML解析程序不会处理,而是直 ...
- 网络数据的XML解析
网络应用中的数据解析,因为最近的应用,无论是Android的和ios平台的,一直用也是建议用的都是Json解析, xml解析都有点被遗忘了. 然后最近自己在做着玩一个ios的小应用,涉及网络数据的抓取 ...
- XML解析【介绍、DOM、SAX详细说明、jaxp、dom4j、XPATH】
什么是XML解析 前面XML章节已经说了,XML被设计为"什么都不做",XML只用于组织.存储数据,除此之外的数据生成.读取.传送等等的操作都与XML本身无关! XML解析就是读取 ...
- xml解析方式之JAXP解析入门
XML解析 1 引入 xml文件除了给开发者看,更多的情况使用[程序读取xml文件]的内容.这叫做xml解析 2 XML解析方式(原理不同) DOM解析 SAX解析 3 XML解析工具 DOM解析原理 ...
随机推荐
- 【在 Nervos CKB 上做开发】Nervos CKB 脚本编程简介[4]:在 CKB 上实现 WebAssembly
作者:Xuejie 原文链接:https://xuejie.space/2019_10_09_introduction_to_ckb_script_programming_wasm_on_ckb/ N ...
- SQL系列(九)—— 子查询(subQuery)
1.子查询 前面的系列介绍的都是简单的查询场景,其中都只涉及到单张表的数据检索.但是在日常是实际应用中,数据模型之间的关系都非常的复杂,数据的需求一般都是来源于多个数据模型之间的组合而成,即对应多张表 ...
- 获取Excel
默认Excel文档为 代码如下 需要下载 "EPPlus.Core" var file = Directory.GetCurrentDirectory() + "\ ...
- Android:Toolbar的图标尺寸问题
之前一直使用的是Material Design的图标库,下载下来以后直接放入了对应文件夹,什么尺寸对应什么dpi都没有仔细研究过. 最近在Toolbar上添加几个不是MD图标库内的图标时发现,放入的图 ...
- python 中 try,except,finally 的执行顺序
写代码的时候发现了好玩的事情,常常作为终止的 return 语句并不总是能够立刻跳出函数 def A(): try: for i in range(10): if i == 5: return pri ...
- FPGA的电源选择重要性分析
米尔Z-Turn Board 7Z010(20)板卡体验有感:IF"> FPGA的电源需求通常很复杂,因为FPGA有多达至少三种供电要求,为了实现可靠的系统性能,必须对这些要求排序. ...
- EXCHANGE上冒充任意用户--Exchange Server权限提升漏洞(CVE-2018-8581)分析
0x00 前言 这是我们2018年Top 5趣案系列中的第三个案例.这些漏洞都有一些因素使它们从今年发布的大约1,400个报告中脱颖而出.今天我们将分析一个Exchange漏洞,它允许任何经过身份验证 ...
- 2 Android程序的执行
Android系统采用的是分层架构,分四层: 1. Applicitations:应用层 2. Applicitation Framework:架构层 3. Libraries:类库层 4. ...
- Vagrant+VirtualBox虚拟环境
Vagrant+VirtualBox虚拟环境 VagrantVirtualBox 软件安装 虚拟机基础配置 虚拟机创建 共享目录 配置网络 配置私有网络 配置公有网络 打包box与添加box 打包bo ...
- Mock Server之接口信息从DB获取
上一篇,写了Mock Server的基础实现与被测系统的对接 当我们mock的接口信息.返回值等时不时维护时,都要在代码中编辑,那体验就不太好了,如果这些可以直接在浏览器编辑就好了. 因此对后端部分做 ...