要解析的xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/regAction" type="test.RegAction">
<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>
java代碼解析前一定要先把jar包導入
接下來我來定義實體類
實體類根據上面的xml去定義。實體類定義的順序一定是由里向外去定義的。
定義順序 forward標簽模型 -> actiong標簽模型 -> config標簽模型
forward標簽模型
public class ForwardModal {
public ForwardModal() {
super();
}
public ForwardModal(String name, String path, boolean redirct) {
super();
this.name = name;
this.path = path;
this.redirct = redirct;
}
private String name;
private String path;
private boolean redirct;
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 isRedirct() {
return redirct;
}
public void setRedirct(boolean redirct) {
this.redirct = redirct;
}
@Override
public String toString() {
return "ForwardModal [name=" + name + ", path=" + path + ", redirct=" + redirct + "]";
}
}
action標簽模型
/**
* Action標簽模型
* @author 20190313
*
*/
public class ActionModal {
private String path;
private String type;
private Map<String, ForwardModal> map = new HashMap<>();
public void addForwardModal(ForwardModal forwardModal) {
//判斷當前的name是不是有重復數據,如果沒有則添加,有則跳過
if(!map.containsKey(forwardModal.getName())) {
map.put(forwardModal.getName(), forwardModal);
}
}
public ForwardModal getForwardModal(String key) {
return map.get(key);
}
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;
}
@Override
public String toString() {
return "ActionModal [path=" + path + ", type=" + type + ", map=" + map + "]";
}
}
config標簽模型
public class ConfigModal {
private Map<String, ActionModal> map = new HashMap<>();
public void addActionModal(ActionModal actionModal) {
if(!map.containsKey(actionModal.getType())) {
map.put(actionModal.getType(), actionModal);
}
}
public ActionModal getActionModal(String key) {
return map.get(key);
}
@Override
public String toString() {
return "ConfigModal [map=" + map + "]";
}
}
將xml解析成java對象的工廠類
記住解析的方式一定是從外往里。先解析 config節點 config節點里面添加action節點。 action節點中添加forward節點
/**
* 配置文件轉成java對象的工廠類
*
* @author 20190313
*
*/
public class ConfigFactory {
private static final String DEFAUL_PATH = "/config.xml";
// 單利模式:只會創建一個對象
private static ConfigModal configModal = null;
/**
* 加載默認路徑下的信息
*
* @return
*/
public static ConfigModal createConfig() {
return createConfig(DEFAUL_PATH);
}
/**
* 加載指定目錄下的信息
* 返回解析xml后等到的對象(ConfigModal)
* @param path
* @return
*/
public static ConfigModal createConfig(String path) {
if (configModal == null) {
configModal = new ConfigModal();
}
// 開始Dom4j解析
SAXReader saxReader = new SAXReader();
try {
//生成指定路徑對應的xml文檔對象
Document document = saxReader.read(ConfigFactory.class.getResourceAsStream(path));
// 讀取ActionModal對象到configModal中
createAction(document, configModal);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return configModal;
}
//這是將action節點解析成ActiongModal對象並放入ConfigModal
private static void createAction(Document document, ConfigModal configModal) {
List actionList = document.selectNodes("/config/action");
//讀取當前action節點的屬性 並對當前action對象的屬性進行賦值
ActionModal actionModal= null;
for (Object object : actionList) {
actionModal = new ActionModal();
Element actionElement = (Element)object;
// 給當前的屬性進行賦值
actionModal.setPath(actionElement.attributeValue("path"));
actionModal.setType(actionElement.attributeValue("type"));
//給當前的action添加ForWord的屬性
createFrowrd(actionElement, actionModal);
configModal.addActionModal(actionModal);
}
}
//將frowrd節點解析成FrowrdModal並放入ActiongModal對象中
private static void createFrowrd(Element actionElement, ActionModal actionModal) {
List forwardList = actionElement.selectNodes("forward");
ForwardModal forwardModal = null;
for (Object object : forwardList) {
forwardModal = new ForwardModal();
Element forwardElement = (Element)object;
forwardModal.setName(forwardElement.attributeValue("name"));
forwardModal.setPath(forwardElement.attributeValue("path"));
forwardModal.setRedirct(Boolean.valueOf(forwardElement.attributeValue("redirect")));
actionModal.addForwardModal(forwardModal);
}
}
}
測試一下
public static void main(String[] args) {
ConfigModal configModal = ConfigFactory.createConfig();
System.out.println(configModal.getActionModal("test.RegAction").getPath());
System.out.println(configModal.getActionModal("test.RegAction").getType());
System.out.println(configModal.getActionModal("test.RegAction").getForwardModal("failed").getPath());
}
控制台打印結果
/regAction
test.RegAction
/reg.jsp