XML建模详解

本文介绍了如何使用Java进行XML数据建模,从最底层的`ForwardModel`开始,逐步构建到`ActionModel`,最后到`ConfigModel`。通过SAXReader解析XML文件,将配置信息映射到对应的实体类中,便于后续操作。主要涉及XML解析、数据建模和Java对象映射技术。
摘要由CSDN通过智能技术生成

XML建模

1.XML建模,就是将XML数据解析出来放入一个专门的实体类中,接下来开始操作。
2.首先是一个XML文件。

<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>

可以看到这个XML是由一个根标签(config)、config下的二个子标签(action),action下分别有二个子标签(forward)。
这里有三个标签首先创建谁呢?
首先创建的依据是:1.该标签没有子标签。2.该标签属于最后一个标签。
创建模型该创建哪些内容呢?
1.我所拥有的属性 。2.我所拥有的子标签。

所以应该先创建forward标签。

public class ForwardModel {

	private String name;
	private String path;
	private boolean redirect=false;//给予默认值
	
	@Override
	public String toString() {
		return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + 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;
	}
	//因为接收的是字符串类型的所以在String写个set方法
	public void setRedirect(String redirect) {
		this.redirect =Boolean.parseBoolean(redirect);
	}
}

3.然后创建forward的上一级标签action


public class ActionModel {
     private String path;
     private String type;
     //将action含有的标签放入到Map集合中
     private Map<String, ForwardModel> forwardModelMap=new HashMap<String, ForwardModel>(); 
     
     //取值
     public ForwardModel getForwardModel(String name) {
    	 if (this.forwardModelMap.containsKey(name)) {
    		 return this.forwardModelMap.get(name);
		}else {
			throw new RuntimeException("该键位name不存在");
		}
    	 
     }
     //放值
 	public void putforwardModel(ForwardModel forwardmodel) {
	     if (null!=forwardmodel) {
			if (!this.forwardModelMap.containsKey(forwardmodel.getName())) {
				this.forwardModelMap.put(forwardmodel.getName(),forwardmodel);
			}else {
				throw new RuntimeException("该Action中已经存在对应的ForWard");
			}
		}else {
			throw new RuntimeException("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;
	}
}

4.最后创建config标签

public class ConfigModel {
    //定义一个静态属性并给予默认值
	private static final String PATH="/config.xml";

	private ConfigModel() {
		
	}
	//将建模方法定义成方法
	public static ConfigModel createConfigModel(String path) throws Exception {
		InputStream is = null;
		假如传过来的path为空就调用上面定义的属性
		if (null==path) {
			is=ConfigModel.class.getResourceAsStream(PATH);
		}else {
			is=ConfigModel.class.getResourceAsStream(path);
		}
		SAXReader sax=new SAXReader();
		Document ment=sax.read(is);
		List<Element> configs = ment.selectNodes("config");
		ConfigModel configModel=new ConfigModel();
		for (Element configelement : configs) {
			List<Element> actions = configelement.selectNodes("action");
			for (Element actionelement : actions) {
				ActionModel actionModel=new ActionModel();
				Attribute actionPath = actionelement.attribute("path");
				Attribute actionType = actionelement.attribute("type");
				actionModel.setPath(actionPath.getValue());
				actionModel.setType(actionType.getValue());
				
				List<Element> forwardsElement = actionelement.selectNodes("forward");
				for (Element forwardelement : forwardsElement) {

					ForwardModel forwardModel=new ForwardModel();
					String forwardName = forwardelement.attribute("name").getValue();
					String forwardPath = forwardelement.attribute("path").getValue();
					String forwarRedirect = forwardelement.attribute("redirect").getValue();
					forwardModel.setName(forwardName);
					forwardModel.setPath(forwardPath);
					forwardModel.setRedirect(forwarRedirect);
					actionModel.putforwardModel(forwardModel);
				}
		        configModel.put(actionModel);
			}
		}
		return configModel;
	}
	
	//定义config的属性
	private Map<String, ActionModel> actionModelMap=new HashMap<String, ActionModel>();
	
	//取值
	public ActionModel get(String path) {
		if (this.actionModelMap.containsKey(path)) {
			return this.actionModelMap.get(path);
		}else {
			throw new RuntimeException("该键位path不存在");
		}
	}
	
	//放值
	public void put(ActionModel actionModel) {
		if (null!=actionModel) {
			if (!this.actionModelMap.containsKey(actionModel.getPath())) {
				this.actionModelMap.put(actionModel.getPath(),actionModel);
			}else {
				throw new RuntimeException("该键位path已经存在");
			}
		}else {
			throw new RuntimeException("参数actionModel不能为空");
		}
	}
	
	
}

5.调用建模方法。

public class Demo {
	public static void main(String[] args) throws Exception {
	  //将XML文件传过去。
		ConfigModel configModel = ConfigModel.createConfigModel("/config.xml");
	    ActionModel actionModel = configModel.get("/loginAction");
	    System.out.println(actionModel.getType());
	}
	
	}

今天的分享就到此结束,各位看官可以在下面评论出自己的看法,大家一起提升,谢谢各位!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值