XML解析与建模
1.XML解析
1)XML解析方式
一、使用SAX解析XML
SAX(Simple API for XML) 使用流式处理的方式,它并不记录所读内容的相关信息。它是一种以事件为驱动的XML API,解析速度快,占用内存少。使用回调函数来实现。 缺点是不能倒退。
二、使用DOM解析XML
DOM(Document Object Model) 是一种用于XML文档的对象模型,可用于直接访问XML文档的各个部分。它是一次性全部将内容加载在内存中,生成一个树状结构,它没有涉及回调和复杂的状态管理。 缺点是加载大文档时效率低下。
三、使用Pull解析XML
Pull内置于Android系统中。也是官方解析布局文件所使用的方式。Pull与SAX有点类似,都提供了类似的事件,如开始元素和结束元素。不同的是,SAX的事件驱动是回调相应方法,需要提供回调的方法,而后在SAX内部自动调用相应的方法。而Pull解析器并没有强制要求提供触发的方法。因为他触发的事件不是一个方法,而是一个数字。它使用方便,效率高。
2) Java中配置文件的三种配置位置及读取方式
- Java中配置文件的三种配置位置及读取方式
1.1 XML和*.properties(属性文件)/ini
1.2 存放位置
1.2.1 src根目录下
Xxx.class.getResourceAsStream("/config.properties");
1.2.2 与读取配置文件的类在同一包
Xxx.class.getResourceAsStream(“config2.properties”);
1.2.3 WEB-INF(或其子目录下)
ServletContext application = this.getServletContext();
InputStream is =
application.getResourceAsStream("/WEB-INF/config3.properties");
注1:*.properties文件
key=value
#注释
Properties.load(is)
3)XML的作用
注:数据交换
4)dom4j+xpath解析xml文件
xpath等同数据库的select语句
document.selectNodes(xpath);//查一组
document.selectSingleNode(xpath);//查单个
DOM由节点组成
Node
元素节点
属性节点
文本节点
xpath
/ 定位路径 在系统中建一个文件叫document/students/student/sid|name
@ 属性
举例:/students/student[@pid=‘p02’]
students.xml
package src;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class Text {
public static void main(String[] args) {
//解析指定xml文件将文件转换为流
InputStream input = Text.class.getResourceAsStream("/dtd.xml");
//定义一个SAXReader对象
SAXReader sa=new SAXReader();
try {
//将流转化为Document对象
Document read = sa.read(input);
//从document中读取所需的东西放入list集合中
List<Element> se = read.selectNodes("/config/action");
//循环读取
for (Element e : se) {
//读取指定节点的属性
String e1 = e.attributeValue("path");
String e2 = e.attributeValue("type");
System.out.println("actionpath:\t"+e1+"type:\t"+e2);
//读取节点
List<Element> s = e.selectNodes("forward");
for (Element el : s) {
//读取其中
String e3 = el.attributeValue("path");
String e4 = el.attributeValue("redirect");
String e5 = el.attributeValue("name");
System.out.println("forwardpath:\t"+e3+"\tredirect:\t"+e4+"\tname:\t"+e5);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.XML建模
定义实体类
config对象
package model;
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
private Map<String, ActionModel> acMap=new HashMap<>();//私立化一个HashMap来储存ActionModel
public void push(ActionModel actionModel) {//存入HashMap集合中
acMap.put(actionModel.getPath(), actionModel);
}
public ActionModel pop(String path) {//根据path从HashMap集合取出元素
return acMap.get(path);
}
}
action对象
package model;
import java.util.HashMap;
import java.util.Map;
public class ActionModel {
// <action path="/loginAction" type="test.LoginAction">
private String path;
private String type;
private Map<String, ForwardModel> fmap=new HashMap<>();//私立化一个HashMap来储存ForwardModel
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(ForwardModel forwardModel) {//将元素存入HashMap集合中
fmap.put(forwardModel.getName(), forwardModel);
}
public ForwardModel pop(String name) {//根据name从HashMap集合取出元素
return fmap.get(name);
}
}
forward对象
package model;
public class ForwardModel {
// <forward name="failed" path="/login.jsp" redirect="false" />
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;
}
}
2.将xml文件中内容封装进model实体对象。
package src;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ConfigFactory {
//默认路径
private static final String ACTION_PATH="/dtd.xml";
private ConfigFactory() {}
//如果没有传入路径就使用默认路径
public static Config createConfig() {
return createConfig(ACTION_PATH);
}
public static Config createConfig(String path) {
//定义一个config对象
Config config=new Config();
//将xml文件转换为流
InputStream resourceAsStream = ConfigFactory.class.getResourceAsStream(path);
//定义一个SAXReader对象
SAXReader sa=new SAXReader();
try {
//将流转化为document对象
Document read = sa.read(resourceAsStream);
//从document中读取节点
List<Element> selectNodes = read.selectNodes("/config/action");
//定义一个action对象
Action ac=new Action();
for (Element e: selectNodes) {
//定义一个forward对象
Forward f=new Forward();
//获取属性
String actionpath= e.attributeValue("path");
String actiontype= e.attributeValue("type");
//添加
ac.setPath(actionpath);
ac.setType(actiontype);
//获取该节点的子节点
List actionforward = e.selectNodes("forward");
for (Element e2 : selectNodes) {
//获取属性
String forwardname = e2.attributeValue("name");
String forwardpath = e2.attributeValue("path");
String forwardredirect = e2.attributeValue("redirect");
//赋值
f.setName(forwardname);
f.setPath(forwardpath);
f.setRedirect(forwardredirect);
ac.push(f);
}
//添加值
config.push(ac);
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return config;
}
//测试
public static void main(String[] args) {
Config createConfig = createConfig();
System.out.println(createConfig.toString());
}
}