[月痕]利用sax解析xml模拟spring的getbean功能

1.创建pojo类

/**
 * 
 */
package testspring.bean;

/**
 * @author 无痕菌
 *
 */
public class Student {
    private int id = 1;
    private String name  = "张三";
    private String clazz = "大四一班";
    /**
     * @return id
     */
    public int getId() {
        return id;
    }
    /**
     * @param id 要设置的 id
     */
    public void setId(int id) {
        this.id = id;
    }
    /**
     * @return name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name 要设置的 name
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return clazz
     */
    public String getClazz() {
        return clazz;
    }
    /**
     * @param clazz 要设置的 clazz
     */
    public void setClazz(String clazz) {
        this.clazz = clazz;
    }}

2.需要解析的beans.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:aop="http://www.springframework.org/schema/aop" 
   xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
">
   
 
 <!--  
   <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/affairs" />
      <property  name="username" value="root"/>
      <property name="password" value="123456"/>
   </bean>
   -->
   <bean id="student" class="testspring.bean.Student" >
       <!-- collaborators and configuration for this bean go here -->
        <property name = "clazz" value = "三年二班"></property>
   </bean>
</beans>

3.sax事件解析,解析测试类

/**
 * 
 */
package springReadBeanTest;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import testspring.bean.Student;

public class SaxXmlGetBean {
    /**
    *作者:alice
    *  日期:2022年1月21日
    *返回值:void
     *参数: @param args
     */
    public static void main(String[] args) {
        
    
        try {
            // 1、创建解析器工厂
            SAXParserFactory factory = SAXParserFactory.newInstance();
            // 2、获得解析器
            SAXParser parser = factory.newSAXParser();
            // SAX解析器 ,继承 DefaultHandler
            String path = new File("src/springReadBeanTest/Beans.xml").getAbsolutePath();
            // 解析
            parser.parse(path, new SAXDemoHandel());
        } catch (SAXException |IOException | ParserConfigurationException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }         
    }
}

class SAXDemoHandel extends DefaultHandler {
    //遍历xml文件开始标签
    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        System.out.println("sax解析开始");
    }

    //遍历xml文件结束标签
    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
        System.out.println("sax解析结束");
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        if (qName.equals("bean")){
            System.out.println("============开始遍历查找需要的bean=============");
            //System.out.println(attributes.getValue("rollno"));
            
            String x = attributes.getValue(1);
            System.err.println(x);
           try {
            Student student =  (Student) Class.forName(x).newInstance();
            System.err.println(student.getId());
        } catch (InstantiationException|IllegalAccessException|ClassNotFoundException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }   
        }
        else if (!qName.equals("bean")&&!qName.equals("class")){
            System.out.print("节点名称:"+qName+"----"); 
        }
        
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        if (qName.equals("student")){
            System.out.println(qName+"遍历结束");
            System.out.println("============结束遍历student=============");
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        String value = new String(ch,start,length).trim();
        if (!value.equals("")) {
            System.out.println(value);
        }
    }
}


 

    
    


 

上一篇:springboot @Import注解 @EnableAutoConfiguration 注解


下一篇:Spring之BeanFactory:解析getBean()方法