xml文件全面解析。

1.什么是xml文件???

xml文件和html文件一样,实际上是一个文本文件。它是一种可扩展标记语言,即简单的数据存储语言。使用一系列简单的标记描述数据,而这些标记可以用方便的方式建立,虽然可扩展标记语言占用的空间比二进制数据要占用更多的空间,但可扩展标记语言极其简单易于掌握和使用。XML的简单使其易于在任何应用程序中读写数据,这使XML很快成为数据交换的公共语言。

2. xml的作用?

1.数据交互。

2.配置文件。

3.标准的xml格式。

 1.有且只有一个跟元素

2.xml标签大小写正确区分

3.正确使用结束标签

4.正确嵌套标签

5.使用了合法的标签名

4. xml元素定义

在xml加入DTD声明——<!DOCTYPE root[]>

元素的分类——:

        <!ELEMENT element-name EMPTY>//空元素

        <!ELEMENT element-name (#PCDATA)>//文本元素

        <!ELEMENT element-name(e1,e2)>//混合元素

元素的限制——:

        与(,)非(|)

        次数——:0或1:?

                            0-N:      *

                             1-N:     +

5.属性定义

语法:<!ATTLIST element-name att_name type desc>

属性类型type:ID.,(男|女),CDATA,IDREF,reference

属性描述:#REQUIRED:必填

                    #IMPLIED:非必填

                    ‘默认值’(只有type为(男|女)类型时,desc才可以用默认值的方式)

示例(xml元素和属性定义):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE persons [
	<!ELEMENT persons (person+)>
	<!ELEMENT person (name,age,contact,br*)>
	<!ELEMENT name (#PCDATA)>
	<!ELEMENT age (#PCDATA)>
	<!ELEMENT contact (phone|email)>
	<!ELEMENT br EMPTY>
	
	<!ATTLIST person 
	pid ID #REQUIRED
	sex (男|女) '男'
	qq CDATA #IMPLIED
	parent IDREF #IMPLIED
	>
]>


<persons>
	<person pid="p1" sex="男" qq="aaa" parent="p2">
		<name>张小明</name>
		<age>10</age>
		<contact>
			<phone>1234567</phone>
		</contact>
		<br/>
	</person>
	<person pid="p2">
		<name>张大明</name>
		<age>35</age>
		<contact>
			<email>123@qq.com</email>
		</contact>
	</person>
</persons>

6.读取xml。

1.定义一个xml文件:

<?xml version="1.0" encoding="UTF-8"?>
	<!--
		config标签:可以包含0~N个action标签
	-->

<!DOCTYPE config [

	<!ELEMENT config (action+)>
	<!ELEMENT action (forward+)>
	<!ATTLIST action 
	path CDATA #REQUIRED
	type CDATA #REQUIRED
	>
	<!ATTLIST forward
	name CDATA #REQUIRED
	path CDATA #REQUIRED
	redirect (true|false) 'false'
	>
]>
	
<config>
	<!--
		action标签:可以饱含0~N个forward标签 path:以/开头的字符串,并且值必须唯一 非空 ,子控制器对应的路径
		type:字符串,非空,子控制器的完整类名
	-->
	<action path="/registerAction" type="test.action.RegisterAction">
		<forward name="success" path="/index.jsp" redirect="true" />
		<forward name="failed" path="/register.jsp" redirect="false" />
	</action>
	<action path="/loginAction" type="test.action.LoginAction">
		<forward name="a" path="/index.jsp" redirect="false" />
		<forward name="b" path="/welcome.jsp" redirect="true" />
	</action>
</config>

先创建一个动态网页项目,点击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<Element> 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<Element> 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包。

7.xml建模

1.把xml通过java文件的方式处理到内存里面。

2.一个标签就是一个对象,也就对应一些属性。

上面xml文件Java对象就有config,action,forward三个,也对于一些属性和方法。

如下:

config类:

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<String,Action> 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);
	}
	
	
}

action类:

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<String,Forward> 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 + "]";
	}
	
	
	
}

forward类:

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<Element> 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<Element> 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读取到内存上面了。

  • 2
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值