目录
1.什么是xml文件?
2. xml的作用?
3.标准的xml格式。
4. xml元素定义
5.属性定义
示例(xml元素和属性定义):
6.读取xml
7.xml建模
config类:
action类:
forward类:
工厂模式:
xml文件和html文件一样,实际上是一个文本文件。它是一种可扩展标记语言,即简单的数据存储语言。使用一系列简单的标记描述数据,而这些标记可以用方便的方式建立,虽然可扩展标记语言占用的空间比二进制数据要占用更多的空间,但可扩展标记语言极其简单易于掌握和使用。XML的简单使其易于在任何应用程序中读写数据,这使XML很快成为数据交换的公共语言。
1.数据交互。
2.配置文件。
1.有且只有一个跟元素
2.xml标签大小写正确区分
3.正确使用结束标签
4.正确嵌套标签
5.使用了合法的标签名
在xml加入DTD声明——<!DOCTYPE root[]>
元素的分类——:
//空元素
//文本元素
//混合元素
元素的限制——:
与(,)非(|)
次数——:0或1:?
0-N: *
1-N: +
语法:
属性类型type:ID.,(男|女),CDATA,IDREF,reference
属性描述:#REQUIRED:必填
#IMPLIED:非必填
‘默认值’(只有type为(男|女)类型时,desc才可以用默认值的方式)
]>
张小明
10
1234567
张大明
35
[email protected]
1.定义一个xml文件:
]>
先创建一个动态网页项目,点击src右击,点击new,点击source folder建一个文件夹,将xml文件放入其中。
定义一个Java文件用来读取xml。
package com.zking.demo;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XMLDemo {
public static void main(String[] args) throws Exception {
InputStream is = XMLDemo.class.getResourceAsStream("/config.xml");
SAXReader reader = new SAXReader();
Document doc = reader.read(is);
Element element = doc.getRootElement();
List list = element.selectNodes("/config/action");
for (Element e1 : list) {
String path = e1.attributeValue("path");
String type = e1.attributeValue("type");
System.out.println(path+"--------"+type);
List list2 = e1.selectNodes("forward");
for (Element e2 : list2) {
String name2 = e2.attributeValue("name");
String path2 = e2.attributeValue("path");
String redirect2 = e2.attributeValue("redirect");
System.out.println(name2+"------"+path2+"------"+redirect2);
}
}
}
}
结果就是:
当然需要一些读取xml文件的jar包。
1.把xml通过java文件的方式处理到内存里面。
2.一个标签就是一个对象,也就对应一些属性。
上面xml文件Java对象就有config,action,forward三个,也对于一些属性和方法。
如下:
package com.zking.mymvc.XMLMedle;
import java.util.HashMap;
import java.util.Map;
import com.zking.mymvc.XMLException.ActionNotFindException;
import com.zking.mymvc.XMLException.ActionNotRepeatException;
public class Config {
private Map map = new HashMap<>();
public void put(Action action) {
if(map.containsKey(action.getPath())) {
throw new ActionNotRepeatException("path:"+action.getPath()+"不能重复!");
}
map.put(action.getPath(), action);
}
public Action find(String path) {
char charAt = path.charAt(0);
if(charAt!='/') {
throw new RuntimeException("path:"+path+"没有以 / 开头!");
}
if(!map.containsKey(path)) {
throw new ActionNotFindException("path:"+path+"没有找到!");
}
return map.get(path);
}
}
package com.zking.mymvc.XMLMedle;
import java.util.HashMap;
import java.util.Map;
import com.zking.mymvc.XMLException.ForwardNotFindException;
import com.zking.mymvc.XMLException.ForwardRepeatException;
public class Action {
private Map map=new HashMap<>();
private String path;
private String type;
public String getPath() {
return path;
}
public void setPath(String path) {
if(path.charAt(0)!='/') {
throw new RuntimeException("path:"+path+"没有以 / 开头!");
}
this.path = path;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void put(Forward forward) {
if(map.containsKey(forward.getName())) {
throw new ForwardRepeatException("name:"+forward.getName()+"不能重复!");
}
map.put(forward.getName(), forward);
}
public Forward find(String name) {
if(!map.containsKey(name)) {
throw new ForwardNotFindException("name:"+name+"没有找到!");
}
return map.get(name);
}
@Override
public String toString() {
return "Action [path=" + path + ", type=" + type + "]";
}
}
package com.zking.mymvc.XMLMedle;
public class Forward {
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(redirect.equals("true") || redirect.equals("false")) {
this.redirect = Boolean.valueOf(redirect);
}else {
throw new RuntimeException("redirect:"+redirect+"不是true和false");
}
}
@Override
public String toString() {
return "Forward [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
}
package com.zking.mymvc.XMLMedle;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public final class XMLModelFactory {
private XMLModelFactory() {
}
private static Config config=new Config();
static {
try {
InputStream is = XMLModelFactory.class.getResourceAsStream("/config.xml");
SAXReader reader = new SAXReader();
Document doc = reader.read(is);
Element element = doc.getRootElement();
List list = element.selectNodes("/config/action");
for (Element e1 : list) {
String path = e1.attributeValue("path");
String type = e1.attributeValue("type");
Action action=new Action();
action.setPath(path);
action.setType(type);
List list2 = e1.selectNodes("forward");
for (Element e2 : list2) {
String name2 = e2.attributeValue("name");
String path2 = e2.attributeValue("path");
String redirect2 = e2.attributeValue("redirect");
Forward f=new Forward();
f.setName(name2);
f.setPath(path2);
f.setRedirect(redirect2);
action.put(f);
}
config.put(action);
}
}catch(Exception e) {
e.printStackTrace();
}
}
public static Config getConfig() {
return config;
}
public static void main(String[] args) {
Config config2 = XMLModelFactory.getConfig();
Action action = config2.find("/registerAction");
System.out.println(action);
Forward forward = action.find("success");
System.out.println(forward);
}
}
这样就把xml读取到内存上面了。