XML文件的解析以及建模

本文探讨了XML文件的解析,采用dom4j库进行解析,并讲解了如何根据XML元素节点创建实体类进行建模。文章通过示例展示了如何解析student.xml和config.xml文件,以及创建对应的config、action和forward实体类。建模过程中,config实体用Map存储action元素,以方便按键快速访问;action和forward实体类对应XML中的属性。
摘要由CSDN通过智能技术生成

上篇文章介绍了XML和dtd的一些知识,本篇文章来介绍一下XML文件的解析以及建模。

1.XML解析

        顾名思义,就是获取xml文件中所有的信息,本章通过dom4j的方式,获取到xml中的信息。

        xml文件:student.xml

<?xml version="1.0" encoding="UTF-8"?>
//dtd约束代码
<!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"
	>
]>
//主要信息
<students>
	<student sid="s001">
		<name>小明</name>
	</student>
	<student sid="s002">
		<name>小芳</name>
	</student>
	<student sid='s003'>
		<name>小王</name>
	</student>
</students>

        解析代码

package com.zking.jeexmlparse;

import java.io.InputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XmlRead {
	public static void main(String[] args) throws Exception {
        //获取student.xml的字节码文件
		InputStream in = XmlRead.class.getResourceAsStream("/students.xml");
		//dom4j提供的类
        SAXReader reader = new SAXReader();
		//读取字节码文件,获得整个文档对象
        Document doc = reader.read(in);
        //获取跟元素
		Element root = doc.getRootElement();
		
        //获取根元素下面所有student子元素
		List<Element> selectNodes = root.selectNodes("student");
        //遍历获取的student子元素
		for(Element e: selectNodes) {
            //获取每个student元素中的sid属性
			String sid = e.attributeValue("sid");
			System.out.println("================");
            //获取到每个student元素中的子元素name,不确定会不会有多个,用集合接收
			List<Element> selectNodes2 = e.selectNodes("name");
            //遍历
			for(Element e2: selectNodes2) {
				//直接打印
                System.out.println(e2.getText());
			}
            //这样也可以,直接省略掉第二个foreach循环
			/*String name = e.selectSingleNode("name").getText();
			System.out.println(name);*/
		}
	}
}

2.建模

        所谓的建模,就是根据xml里面元素的节点,去创建实体,然后再把解析的详细,放到这些实体里面,这样就可以使用了

        xml文件:config.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"/>
		<forward name="students02" path="/students/studentList02.jsp" redirect="false"/>
	</action>
	<action path="/studentAction1" type="org.lisen.mvc.action.StudentAction">
		<forward name="students1" path="/students/studentList.jsp" redirect="false"/>
		<forward name="students021" path="/students/studentList02.jsp" redirect="false"/>
	</action>
</config>

        根据里面的元素,可以看出最少需要3个实体

        config实体类:

package com.zking.mvc.framework;

import java.util.HashMap;
import java.util.Map;

import com.zking.mvc.exception.ActionDuplicataDefinitionException;
import com.zking.mvc.exception.ActionNotFounndException;

public class ConfigModel {
	
	private Map<String, ActionModel> ActionMap = new HashMap<>();
	
	public void put(ActionModel actionModel) {
		if(ActionMap.containsKey(actionModel.getPath())) {
			throw new ActionDuplicataDefinitionException("action path :"+actionModel.getPath()+" 不能重复");
		}
		ActionMap.put(actionModel.getPath(), actionModel);
	}
	
	public ActionModel find(String path) {
		if(!ActionMap.containsKey(path)) {
			throw new ActionNotFounndException("action path :"+path+" 没有找到");
		}
		return ActionMap.get(path);
	}
	
	
}

        这个类为什么就一个Map集合呢?因为他不来就是根节点,没有属性可言,然后config下面可能有很多个action子节点所以,为了体现出对应关系,所以要一个map集合存放action元素。为什么又是map集合呢?list不行吗?原因是,map集合可以直接根据键找到值,则list集合需要遍历对比,相比下map集合更好适合一点。

        put方法,就是往集合中存放数据,由于map集合键不能重复,所以先需要判断一下集合中是否有这个键,如果有则抛出一个自定义异常。

        find方法就是直接根据键得到值。抛出的自定义异常就是判断map集合中是否有这个键。

        action实体类:

package com.zking.mvc.framework;

import java.util.HashMap;
import java.util.Map;

import com.zking.mvc.exception.ActionDuplicataDefinitionException;
import com.zking.mvc.exception.ActionNotFounndException;
import com.zking.mvc.exception.ForwardDuplicataDefinitionException;
import com.zking.mvc.exception.ForwardNotFounndException;

public class ActionModel {
	private String path;
	
	private String type;
	
	private Map<String, ForwardModel> forwardMap = new HashMap<>();
	
	public void put(ForwardModel forwardModel) {
		if(forwardMap.containsKey(forwardModel.getName())) {
			throw new ForwardDuplicataDefinitionException("forward name :"+forwardModel.getName()+" 不能重复");
		}
		forwardMap.put(forwardModel.getName(), forwardModel);
	}
	
	public ForwardModel find(String name) {
		if(!forwardMap.containsKey(name)) {
			throw new ForwardNotFounndException("forward name :"+name+" 没有找到");
		}
		return forwardMap.get(name);
	}
	
	
	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 "ActionModel [path=" + path + ", type=" + type + "]";
	}
	
	
	
	
}

        path和type属性就是xml中action元素中的属性。

        forward实体类:

package com.zking.mvc.framework;

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) {
		this.redirect = Boolean.valueOf(redirect);
	};
	
	
	@Override
	public String toString() {
		return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
	}  
	
	
	

}

        单例模式工厂和测试

package com.zking.mvc.framework;

import java.io.InputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;



public final class ConfigModelFactory {
	
	private ConfigModelFactory() {}
	
	private static ConfigModel config = new ConfigModel();
	
	static {
		
		try {
			InputStream in = ConfigModelFactory.class.getResourceAsStream("/config.xml");
			
			SAXReader reader = new SAXReader();
			Document doc = reader.read(in);
			Element element = doc.getRootElement();
			List<Element> actions = element.selectNodes("action");
			for (Element action : actions) {
				String path = action.attributeValue("path");
				String type = action.attributeValue("type");
				ActionModel ac = new ActionModel();
				ac.setPath(path);
				ac.setType(type);
				config.put(ac);
				List<Element> forward = action.selectNodes("forward");
				for (Element f : forward) {
					String name = f.attributeValue("name");
					String path1 = f.attributeValue("path");
					String redirect = f.attributeValue("redirect");
					ForwardModel forwardModel = new ForwardModel();
					forwardModel.setName(name);
					forwardModel.setPath(path1);
					forwardModel.setRedirect(redirect);
					ac.put(forwardModel);
				}
			}
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	
	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);
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值