xml文件
<LcSpring>
<s1 classname="com.java.zlc.Student">
<Student>
<property name="rollno">393</property>
<property name="firstname">住宿费</property>
<property name="marks">85</property>
</Student>
</s1>
<t1 classname="com.java.zlc.Teacher">
<Teacher>
<property name="id">12</property>
<property name="name">纽卡</property>
</Teacher>
</t1>
</LcSpring>
xml解析核心类
package com.java.zlc;
import java.io.File;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
import com.java.zlc.Teacher;
public class LcSpring {
//单个对象注入
public static void main(String[] args) {
LcSpring lcSpring=new LcSpring();
Student student=(Student) lcSpring.getBean("s1");
System.out.println(student);
Teacher teacher=(Teacher) lcSpring.getBean("t1");
System.out.println(teacher);
}
public Object getBean(String id) {
String classname = "";
Object object = null;
Map map = new HashMap();
try {
File inputFile = new File("LcSpring.xml");
// 1.create DocumentBuilder object
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
// 2.create Document object
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
// get root node
// Node node=doc.getFirstChild();
String nameString = doc.getFirstChild().getNodeName();
if (!nameString.equals("LcSpring")) {
throw new Exception("未发现LcSpring");
}
Node node = doc.getElementsByTagName(id).item(0);
classname = ((Element) node).getAttribute("classname");
// 3.get student list;
String[] classtr = classname.split("\\.");
String myclass = classtr[classtr.length - 1];
NodeList nList = doc.getElementsByTagName(myclass);
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
NodeList nodeList = eElement.getElementsByTagName("property");
for (int i = 0; i < nodeList.getLength(); i++) {
String value = nodeList.item(i).getTextContent();
String property = ((Element) nodeList.item(i)).getAttribute("name");
map.put(property, value);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
try {
object = Class.forName(classname).getConstructor().newInstance();
object = setValue(object, map);
} catch (Exception e) {
e.printStackTrace();
}
return object;
}
public Object setValue(Object object, Map map) {
try {
for (Object key : map.keySet()) {
String value = (String) map.get(key);
Field f1 = object.getClass().getDeclaredField((String) key);
f1.setAccessible(true);
String type = f1.getType().toString();// 得到此属性的类型
if (type.endsWith("int") || type.endsWith("Integer")) {
f1.set(object, Integer.parseInt(value));
} else {
f1.set(object, value);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return object;
}
}
实体类
package com.java.zlc;
public class Student {
private int rollno;
private String firstname;
private String marks;
@Override
public String toString() {
return "Student [rollno=" + rollno + ", firstname=" + firstname
+ ", marks=" + marks + "]";
}
}