XML解析和建模

本文详细介绍了XML在配置和数据交互中的作用,并讲解了Java中读取.properties和.xml配置文件的方法。通过DOM4J库,演示了XPath在XML解析中的应用,以及如何使用工厂模式进行XML建模,包括配置文件解析和对象映射。最后,提供了一个具体的XML建模案例,展示了如何从XML配置文件构建Action和Forward对象模型。
摘要由CSDN通过智能技术生成

1.XML作用
   1.1配置 (保存数据)
   *.xml和*.properties INI   yml  springboot

   1.2数据交互(获取第三方数据)
    webservice   xml  ajax    json
   

2.Java中3种配置位置及读取方式
   2.1如何使用Properties读取配置文件
      1)*.properties文件以键值对的方式存储数据;
      2)使用Properties类读取配置文件;
    

   2.2 配置位置
      1)存放于根目录下,/代表获取src根目录的绝对路径
      2)存放于同一类的包下,不加/代表同类名包下的相对路径;
      3)存放于WEB-INF目录下

//1.获取io流
        InputStream is = Demo03.class.getResourceAsStream(需要解析的xml绝对路径);

dom4j+xpath解析xml文件  document   for  java   SAX   
    1)xpath类似数据库中的select语句;
    2)Document有节点(Node)组成:元素.节点、属性、文本等
    3)  Node(节点)  Element(元素) 区别  getText();
    3)selectNodes()获取节点下所有子节点;
    4)selectSingleNode()获取单个节点信息;
    5) xpath语法:/(定位路径)、@(获取属性)

案例 1 xml解析

 //1.获取io流
        InputStream is = Demo03.class.getResourceAsStream("/students.xml");
//如果is输出的值不为空就代表获取到了,不然就要看看路径错了没
        System.out.println(is);

        //2.创建SaxReader对象 读取器对象
        SAXReader sr = new SAXReader();

        //3.读取配置文件,获取Document对象
        Document read = sr.read(is);

        //4.使用xpath解析document对象
        Node node = read.selectSingleNode("/students/student[@sid='s001']");

        System.out.println(node);

        Element el=(Element) node;
        String sid = el.attributeValue("sid");

        System.out.println(sid);

 

1.什么叫XML建模
将XML配置文件中的元素、属性、文本信息转换成对象的过程叫做XML建模

2. XML建模
   1)根据XML配置文件元素节点创建元素节点实体类
   ConfigModel、ActionModel、ForwardModel
   2)利用dom4j+xpath技术实现XML建模
   ConfigModelFactory

 案例2 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"
	>
]>
	<!--Action
Config
Forward
		config标签:可以包含0~N个action标签
	-->
<config>
	<!--
		action标签:可以饱含0~N个forward标签
		path:以/开头的字符串,并且值必须唯一 非空
		type:字符串,非空
	-->
	<action path="/regAction" type="test.RegAction">
		<!--
			forward标签:没有子标签; 
			name:字符串,同一action标签下的forward标签name值不能相同 ;
			path:以/开头的字符串
			redirect:只能是false|true,允许空,默认值为false
		-->
		<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>

思路:
1)xml文件config.xml

2)根据XML中元素节点情况(DTD)来定义

ConfigModel属性:(Map<ActionModel.path,ActionModel>())

ActionModel属性:(path,type,Map<ForwardModel.name,ForwardModel>)

ForwardModel属性:(name,path,redirect)

对象模型
   
   A.config节点下有多个子action节点,无节点属性
   B.action节点下有多个子forward节点,有节点属性
   C.forward下无子节点,有节点属性

3)使用Map集合存放子节点元素,其中key为子节点唯一属性,value为整个子节点对象


4)利用工厂模式+dom4j+xpath解析Xml配置文件

 

 

package com.zking.utils;

import com.zking.entity.Action;
import com.zking.entity.Config;
import com.zking.entity.Forward;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

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

public class ConfigGc {

    public static final String Path_Name ="/config.xml";

  public static Config get(){
      //定义变量
      Config c = new Config();
      Action a = null;
      Forward f = null;
      String actionType = null;
      String actionPath = null;

      String forwardName = null;
      String forwardPath = null;
      String forwardRedirect = null;
        try {
            //获取io流
            InputStream is = ConfigGc.class.getResourceAsStream(Path_Name);
//            System.out.println(is);
            //创建读取器对象
            SAXReader sr = new SAXReader();
            //获取document对象
            Document doc = sr.read(is);
            //遍历action节点 并将path type 设置到action对象中
            List<Node> actionNode = doc.selectNodes("/config/action");
            for (Node action: actionNode) {
                Element actionEl = (Element) action;
                //实例化一个action对象
                a = new Action();
                //根据属性名得到文本值
                actionPath = actionEl.attributeValue("path");
                actionType = actionEl.attributeValue("type");
                //将actionpath 和 actiontype 设置到action对象
                a.setPath(actionPath);
                a.setType(actionType);
                //通过action节点找到forward节点 并将name path redirect 设置到forward对象中
                List<Node> forwardNode = action.selectNodes("forward");
                //遍历forward节点
                for (Node forward: forwardNode) {
                    Element forwardEl = (Element) forward;
                    //实例化一个forward对象
                    f = new Forward();
                    //根据属性名得到文本值
                    forwardName = forwardEl.attributeValue("name");
                    forwardPath = forwardEl.attributeValue("path");
                    forwardRedirect = forwardEl.attributeValue("redirect");
                    //将forwardname、forwardpath、forwardredirect设置到forward对象
                    f.setName(forwardName);
                    f.setPath(forwardPath);
                    f.setRedirect(Boolean.parseBoolean(forwardRedirect));
                    //将forward对象添加到action对象中的map集合
                    a.put(f);
                }
                //将action对象添加到config对象中的map集合
                c.put(a);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return c;
    }



   public static void main(String[] args) {
        Config cc = ConfigGc.get();
        Action aa = cc.get("/regAction");
       System.out.println(aa.getType());

       Forward ff = aa.get("success");
       System.out.println(ff.getPath());
       System.out.println(ff.isRedirect());

   }
}

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值