JDom lib 下载地址:http://www.jdom.org/downloads/index.html
BeanFactory.xml
<?xml version="1.0" encoding="UTF-8"?> <Beans> <bean id="Car" class="com.rain.Factory.Car"> </bean> <bean id="SuperCar" class="com.rain.Factory.SuperCar"> </bean> </Beans>
IVehicle 接口
package com.rain.Factory; public interface IVehicle { void run(); }
Car
package com.rain.Factory; public class Car implements IVehicle { @Override public void run() { System.out.println("小汽车跑跑跑"); } @Override public String toString() { return "Car"; } }
SuperCar
package com.rain.Factory; public class SuperCar implements IVehicle { @Override public void run() { System.out.println("超级跑车"); } }
IBeanFactory
package com.rain.Factory; public interface IBeanFactory { Object getBean(String key); }
ClassPathXmlApplicationContext
package com.rain.Factory; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import org.jdom2.*; import org.jdom2.input.SAXBuilder; import org.jdom2.xpath.XPath; public class ClassPathXmlApplicationContext implements IBeanFactory { private Map<String, Object> container=new HashMap<String, Object>(); public ClassPathXmlApplicationContext(String fileName) throws JDOMException, IOException{ SAXBuilder sb = new SAXBuilder(); //拿到要读取的文件 Document doc = sb.build(this.getClass().getClassLoader().getResource(fileName)); Element root = doc.getRootElement(); //直接难道要读取节点,而不是像传统那样一层层循环查找到节点在读取 List list = XPath.selectNodes(root, "/Beans/bean"); for (int i = 0; i < list.size(); i++) { Element bean_element = (Element) list.get(i); String id = bean_element.getAttributeValue("id"); String clazz=bean_element.getAttributeValue("class"); try { Object object = Class.forName(clazz).newInstance(); this.container.put(id, object); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } @Override public Object getBean(String key) { return this.container.get(key); } public static void main(String[] args) throws JDOMException, IOException { ClassPathXmlApplicationContext c=new ClassPathXmlApplicationContext("com/rain/Factory/BeanFactory.xml"); Object o=c.getBean("Car"); IVehicle vehicle=(IVehicle) o; vehicle.run(); } }
Factory
package com.rain.Factory; import java.io.IOException; import org.jdom2.JDOMException; public class Factory { public static void main(String[] args) throws JDOMException, IOException { IBeanFactory factory=new ClassPathXmlApplicationContext("com/rain/Factory/BeanFactory.xml"); // IVehicle vehicel=(IVehicle) factory.getBean("Car"); IVehicle vehicel=(IVehicle) factory.getBean("SuperCar"); vehicel.run(); } }
Xpath 实例代码
Sample.xml
<?xml version="1.0" encoding="UTF-8"?> <HD> <disk name="C"> <capacity>8G</capacity> <directories>200</directories> <files>1580</files> </disk> <disk name="D"> <capacity>10G</capacity> <directories>500</directories> <files>3000</files> </disk> </HD>
Sample
package com.rain.Factory; import java.io.IOException; import java.util.List; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.Text; import org.jdom2.input.SAXBuilder; import org.jdom2.xpath.XPath; public class Sample { public Sample() throws JDOMException, IOException { SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(this.getClass().getClassLoader().getResource("com/rain/Factory/Sample.xml")); Element root = doc.getRootElement(); List list = XPath.selectNodes(root, "/HD/disk"); for (int i = 0; i < list.size(); i++) { Element disk_element = (Element) list.get(i); String name = disk_element.getAttributeValue("name"); String capacity = ( (Text) XPath.selectSingleNode(disk_element, "//disk[@name='" + name + "']/capacity/text()")).getTextNormalize(); String directories = ( (Text) XPath.selectSingleNode(disk_element, "//disk[@name='" + name + "']/directories/text()")).getTextNormalize(); String files = ( (Text) XPath.selectSingleNode(disk_element, "//disk[@name='" + name + "']/files/text()")).getTextNormalize(); System.out.println("磁盘信息:"); System.out.println("分区盘符:" + name); System.out.println("分区容量:" + capacity); System.out.println("目录数:" + directories); System.out.println("文件数:" + files); System.out.println("-----------------------------------"); } } public static void main(String[] args) throws JDOMException, IOException { new Sample(); } }