[黄昏]借助jdom解析xml模拟spring的getbean功能
作者:互联网
1.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>
2.创建pojo类student
/**
*
*/
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;
}
}
3.利用jdom模拟spring的getbean功能
//解析xml文件,获取相关节点的属性,并通过反射实例化
/**
*
*/
package springReadBeanTest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import testspring.bean.Student;
/**
* @author 无痕菌
*
*/
public class JdomParseXmlSpringGetBean {
/**
*作者:alice
* 日期:2022年1月21日
*返回值:void
*参数: @param args
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws IOException
* @throws JDOMException
*/
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, JDOMException, IOException {
/*
* JDOM 是一种使用 XML 的独特 Java 工具包,用于快速开发 XML 应用程序。JDOM 是一个开源项目,
* 它基于树形结构,利用纯 Java 的技术对 XML 文档实现解析、生成、序列化及多种操作。
*/
// 获取SAX解析器
SAXBuilder builder = new SAXBuilder();
File file = new File("src/springReadBeanTest/Beans.xml");
// 获取文档
Document doc = builder.build(new File(file.getAbsolutePath()));
// 获取根节点
Element root = doc.getRootElement();
System.out.println(root.getName());
// 获取根节点下所有的子节点, 也可以根据标签名称获取指定的直接点
List<Element> list = root.getChildren();
System.out.println(list.size());
for(int x = 0; x<list.size(); x++){
Element e = list.get(x);
if(e.getName().contentEquals("bean")){
String aString = e.getAttribute("class").getValue();
Student student = (Student) Class.forName(aString).newInstance();
System.err.println(student.getName());
break;
}
/* 获取元素的名称和里面的文本
System.out.println(name + "=" + e.getText());
System.out.println("==================");}
// 创建节点
Element person = new Element("person");
Element name = new Element("name");
Element age = new Element("age");
// 创建属性
Attribute id = new Attribute("id","1");
// 设置文本
name.setText("shsxt");
age.setText("10");
// 设置关系
Document doc = new Document(person);
person.addContent(name);
name.setAttribute(id);
person.addContent(age);
XMLOutputter out = new XMLOutputter();
File file = new File("src/springReadBeanTest/Beans.xml");
out.output(doc, new FileOutputStream(file.getAbsoluteFile()))
*/
}
}
}
标签:xml,www,name,spring,jdom,springframework,new,org,id 来源: https://blog.csdn.net/u010050660/article/details/122625762