目录
创建模型:ConfigModel、ActionModel、ForwardModel
一、dtd
1、了解xml
问题:什么是xml?为什么学习xml?xml的作用?
DTD为英文Document Type Definition,中文意思为“文档类定义”。
DTD是一套关于标记的语法规则,它说明了在XML中哪些标记可以使用、哪些标记具有属性以及使用标记出现的顺序是什么。
DTD肩负着两重任务:
1、帮助编写出合法的代码;
2、让浏览器正确地显示器代码。
- XML 指可扩展标记语言(EXtensible Markup Language)
- XML 是一种标记语言,很类似 HTML
- XML 的设计宗旨是传输数据,而非显示数据
- XML 标签没有被预定义。您需要自行定义标签。
- XML 被设计为具有自我描述性。
- XML 是 W3C 的推荐标准
后期学习mvc配置文件
为了便于不用应用、不同平台之间的数据共享和通信。
作用:网络数据传输.
数据存储
配置文件
2、xml语法格式
- 有且只有一个根元素
- xml标签大小写正确区分
- 正确使用结束标签、嵌套标签
- 使用合法的标签名
- 定义有效的属性
- 开始标记(开放标记): <标记名称>
- 结束标记(闭合标记): </标记名称>
例如:<name>张三</name>
3、元素定义
标签元素命名语法规则:
1.名称可以含字母、数字以及其他的字符
2.名称不能以数字或者标点符号开始
3.名称不能以字符 “xml”(或者 XML、Xml)开始
4.名称不能包含空格,不能包含冒号(:)
5.名称区分大小写
在XML加入DTD声明 <!DOCTYPE root[]>
元素的分类:
<!ELEMENT element-name (#PCDATA)> ------------文本元素
<!ELEMENT element-name EMPTY> ----------------空元素
<!ELEMENT element-name (e1,e2)> ----------------混合元素
元素限制:
与 ( , ) 非 ( | )
次数:0或1------?
0~N-------*
1~N-------+
//案例
<!DOCTYPE persons[
<!ELEMENT persons (person+)>
<!ELEMENT person (name,age,contact,br*)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT contact (phone|email)>
<!ELEMENT br EMPTY>
]>
3、属性定义
语法:
<!ATTLIST element-name
att_name type desc
>
属性类型:
ID (男|女) CDATA IDREF reference
属性描述:
#REQUIRED 必填
#IMPLED 非必填
'默认值'(只有type为(男|女)类型时,desc才可以用默认值的方式)
//案例
<!ATTLIST person
pid ID #REQUIRED
sex (男|女) '男'
qq CDATA #IMPLIED
parent IDREF #IMPLIED
>
二、XML的解析
1、java中配置文件的三种配置位置及读取方式
同包:Demo1.class.getResourceAsStream("db.properties");
根路径:Demo1.class.getResourceAsStream("/db.properties");
WIN-INF安全路径:context.getResourceAsStream("/WEB-INF/db.properties");
2、dom4j的使用
dom4j常用方法:selectNodes selectSingleNode attributeValue getText
配置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>
<action path="/studentAction" type="org.lisen.mvc.action.StudentAction">
<forward name="students" path="/students/studentList.jsp" redirect="false"/>
</action>
<action path="/studentAction1" type="org.lisen.mvc.action.StudentAction">
<forward name="students1" path="/students/studentList.jsp" redirect="false"/>
</action>
<action path="/studentAction2" type="org.lisen.mvc.action.StudentAction">
<forward name="students2" path="/students/studentList.jsp" redirect="false"/>
</action>
</config>
编写获取方法
public class Demo {
public static void main(String[] args) throws Exception {
InputStream stream = Demo.class.getResourceAsStream("/config.xml");
SAXReader reader = new SAXReader();// SAXReader 读取xml文件解析
Document document = reader.read(stream);// 文档
Element element = document.getRootElement();// 获取根元素
List<Element> actions = element.selectNodes("/config/action");
for (Element action : actions) {
String path = action.attributeValue("path");
String type = action.attributeValue("type");
List<Element> forwards = action.selectNodes("forward");
for (Element forward : forwards) {
String name = forward.attributeValue("name");
String fpath = forward.attributeValue("path");
String redirect = forward.attributeValue("redirect");
System.out.println("name=" + name);
System.out.println("fpath=" + fpath);
System.out.println("redirect=" + redirect);
}
System.out.println("path=" + path);
System.out.println("type=" + type);
System.out.println("---------------");
}
}
}
运行结果如下:
三、建模
1、xml建模
创建模型:ConfigModel、ActionModel、ForwardModel
ConfigModel
public class ConfigModel {
private Map<String, ActionModel> actionMap = new HashMap<>();
public void put(ActionModel action) {
if (actionMap.containsKey(action.getPath())) {
throw new ActionDuplicateDefinitionException("action path:" + action.getPath() + " 不能重复");
}
actionMap.put(action.getPath(), action);
}
public ActionModel find(String path) {
if (!actionMap.containsKey(path)) {
throw new ActionNotFoundException("action path:" + path + "没有找到");
}
return actionMap.get(path);
}
}
ActionModel
public class ActionModel {
private String path;
private String type;
private Map<String, ForwardModel> forwardMap = 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;
}
public void put(ForwardModel forward) {
if (forwardMap.containsKey(forward.getName())) {
throw new ForwardDuplicateDefinitionException("forward name:" + forward.getName() + "不能重复");
}
forwardMap.put(forward.getName(), forward);
}
public ForwardModel find(String name) {
if (!forwardMap.containsKey(name)) {
throw new ForwardNotFoundException("forward name:" + name + "不存在");
}
return forwardMap.get(name);
}
@Override
public String toString() {
return "ActionModel [path=" + path + ", type=" + type + "]";
}
}
ForwardModel
public class ForwardModel {
private String name;
private String path;
private boolean 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 boolean isRedirect() {
return redirect;
}
public void setRedirect(boolean redirect) {
this.redirect = redirect;
}
public void setRedirect(String redirect) {
if ("true".equals(redirect) || "false".equals(redirect)) {
this.redirect = Boolean.valueOf(redirect);
} else {
throw new RuntimeException("属性redirect的值必须位ture或者false");
}
}
@Override
public String toString() {
return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
}
2、工厂模式
解析并生成模型
ConfigModelFactory
public final class ConfigModelFactory {
private ConfigModelFactory() {
}
private static ConfigModel config = new ConfigModel();
// 静态代码块,在类加载的时候只执行一次,在这里完成config.xml文件的解析和Config相关的
// 模型的建立,这样在整个系统运行阶段只会解析一次
static {
try {
InputStream in = ConfigModelFactory.class.getResourceAsStream("/config.xml");
SAXReader reader = new SAXReader();
Document doc = reader.read(in);
Element ce = doc.getRootElement();
List<Element> actions = ce.selectNodes("action");
for (Element action : actions) {
String path = action.attributeValue("path");
String type = action.attributeValue("type");
ActionModel actionModel = new ActionModel();
actionModel.setPath(path);
actionModel.setType(type);
List<Element> forwards = action.selectNodes("forward");
for (Element f : forwards) {
String name = f.attributeValue("name");
String fpath = f.attributeValue("path");
String redirect = f.attributeValue("redirect");
ForwardModel forwardModel = new ForwardModel();
forwardModel.setName(name);
forwardModel.setPath(fpath);
forwardModel.setRedirect(redirect);
actionModel.put(forwardModel);
}
config.put(actionModel);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// 获取系统的Config配置对象
public static ConfigModel getConfig() {
return config;
}
public static void main(String[] args) {
ConfigModel config = ConfigModelFactory.getConfig();
ActionModel action = config.find("/studentAction");
System.out.println(action);
ForwardModel forward = action.find("students");
System.out.println(forward);
}
}
运行结果如下: