您的位置:首页 > 产品设计 > UI/UE

SAXBuilder解析

2018-11-11 18:01 106 查看

SAXBuilder是什么?
SAXBuilder是一个JDOM解析器,能够将路径中xml文件解析到Document对象

例:
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(path);

使用SAXBuilder的优缺点
优点:对内存消耗小,适用于只处理xml文件
缺点:不易编程(需要借助handler来进行解析)and 很难同时访问一个xml中的多处不同数据(事件有先后顺序)

SAXBuilder解析xml实例

beans.xml文件

解析xml的类

package com.ss.service;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.ss.dao.BookDaoImpl;
import com.ss.simulation.ApplicationContext;

public class ClassPathXmlApplicationContext implements ApplicationContext {

private Map<String, Object> container = new HashMap<String, Object>();

public Map<String, Object> getContainer() {
return container;
}

public void setContainer(Map<String, Object> container) {
this.container = container;
}

@Override
public Object getBean(String id) {
return container.get(id);
}

// 构造器
public ClassPathXmlApplicationContext(String path) {
// 创建SAXBuilder解析器
SAXBuilder builder = new SAXBuilder();
Document document = null;

try {
// 得到Document,之后的操作都是对它进行操作    build(path):加载XML文件
document = builder.build(ClassPathXmlApplicationContext.class.getClassLoader().getResourceAsStream(path));
// 获取根节点
Element root = document.getRootElement();
// 获取根节点下面名为“bean”的子节点
List<Element> children = root.getChildren("bean");

for (Element child : children) {
String key = child.getAttributeValue("id");
String classValue = child.getAttributeValue("class");
// Class.forName():返回一个类     newInstance():获取实例
Object value = Class.forName(classValue).newInstance();
System.out.println("key:"+key+"   classValue:"+classValue);
container.put(key, value);
}

for (Element child : children) {
Object obj = container.get(child.getAttributeValue("id"));
List<Element> properties = child.getChildren("property"); // 得到子节点下面名为“property”的子节点

// 处理property标签,对应对象时被注入的对象
for (Element property : properties) {
String ref = property.getAttributeValue("ref"); // 获取property标签里ref属性
String setStr = "";
Object value = null;
Method setter = null;

if (null != ref) {
// toUpperCase():把所有字母转换成大写   toLowerCase():把所有字母转换成小写
// substring(x):从x下标开始截取字符
// substring(x, y):从x下标开始截取到y下标结束,包含x下标的字符,不包含y下标的字符,左臂右开
setStr = "set".concat(ref.substring(0, 1).toUpperCase()).concat(ref.substring(1));
System.out.println("set:"+setStr);
value = container.get(ref);
System.out.println("key:"+value);
// getInterfaces()[0]:获得对象所实现的第一个接口
setter = obj.getClass().getMethod(setStr, value.getClass().getInterfaces()[0]);
setter.invoke(obj, value);
} else {
String name = property.getAttributeValue("name");
String v = property.getAttributeValue("value");
setStr = "set".concat(name.substring(0, 1).toUpperCase()).concat(name.substring(1));
System.out.println("name:   "+name+"     setStr:   "+setStr);
Field field = obj.getClass().getDeclaredField(name);
// field.setAccessible(true);

// getGenericType():如果当前属性有签名属性就返回,否则就返回 field.getType()
// getType():返回一个class对象   class java.lang.Integer
Type type = field.getGenericType();
System.out.println("type.toString:    " + type.toString());
if (type.toString().endsWith("Integer")) {  // 判断后面是否以指定参数结束
setter = obj.getClass().getDeclaredMethod(setStr, Integer.class);
Integer va = Integer.parseInt(v);
setter.invoke(obj, va);
} else if (type.toString().endsWith("String")) {
setter = obj.getClass().getDeclaredMethod(setStr, String.class);
setter.invoke(obj, v);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
ClassPathXmlApplicationContext context = new  ClassPathXmlApplicationContext("beans.xml");
BookDaoImpl bean = (BookDaoImpl)context.getBean("bookDao");
System.out.println(bean);
}

}

实体类

package com.ss.dao;

public class BookDaoImpl implements BookDao {

private String name;
private Integer age;
public BookDaoImpl(String name, Integer age) {
this.name = name;
this.age = age;
}
public BookDaoImpl() {
}
@Override
public String toString() {
return "BookDaoImpl [name=" + name + ", age=" + age + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}

}

控制台

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: