java利用dom/jdom解析XML

xml: 

<?xml version="1.0" encoding="UTF-8"?>
<beans>

	<bean id="v" class="com.bjsxt.spring.factory.Train">
	</bean>


</beans>

jdom:

package com.bjsxt.spring.factory;

import java.io.IOException;
import java.util.List;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

public class JDomTest implements BeanFactory {
	public static void main(String[] args) {
		JDomTest test = new JDomTest("src/com/bjsxt/spring/factory/applicationContext.xml");
		
		System.out.println(test.getValue("v"));
	}

	@Override
	public Object getBean(String id) {
		String typeName = this.getValue(id);
		if (null != typeName)
			try {
				return Class.forName(typeName).newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}

		return null;
	}

	public JDomTest(String fileName) {
		this.fileName = fileName;
	}

	private String fileName;

	private String getValue(String key) {
		SAXBuilder builder = new SAXBuilder();
		Document document = null;
		try {
			document = builder.build(fileName);
			Element root = document.getRootElement();
			List<Element> list = root.getChildren();

			for (int i = 0; i < list.size(); i++) {
				Element ele = list.get(i);

				/*
				 * for (Attribute attr : ele.getAttributes()) {
				 * System.out.println(attr.getName() + "/" + attr.getValue()); }
				 */
				
				if (ele.getAttributeValue("id").equals(key)) {
					return ele.getAttributeValue("class");
				}
			}
		} catch (JDOMException |

				IOException e) {
			e.printStackTrace();
		}

		return null;
	}
}

DOM:

package com.bjsxt.spring.factory;

import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ClassPathXmlApplicationContext implements BeanFactory {

	private Map<String,Object> container = new HashMap<String,Object>();
	
	public ClassPathXmlApplicationContext(String fileName) throws Exception {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document document = db.parse(fileName);
		NodeList beanList = document.getElementsByTagName("bean");

		for (int i = 0; i < beanList.getLength(); i++) {
			Node bean = beanList.item(i);
			NamedNodeMap attrs = bean.getAttributes();
			String id= null;
			String clazz = null;

			for (int j = 0; j < attrs.getLength(); j++) {
				Node attr = attrs.item(j);

				if (attr.getNodeName().contentEquals("id")) id = attr.getNodeValue();
				if (attr.getNodeName().contentEquals("class")) clazz = attr.getNodeValue();
			}
			container.put(id, Class.forName(clazz).newInstance());
		}
		
	}

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

}

TEST:

package com.bjsxt.spring.factory;

public class Test {

	public static void main(String[] args) throws Exception {
		/*
		 * Properties props = new Properties(); props.load(Test.class.getClassLoader()
		 * .getResourceAsStream("com/bjsxt/spring/factory/spring.properties")); String
		 * vehicleTypeName = props.getProperty("VehicleType"); 
		 * Object o = Class.forName(vehicleTypeName).newInstance();
		 */
		// String fileName = "D:\\TestJava\\Factory\\bin\\com\\bjsxt\\spring\\factory";

		// BeanFactory f = new ClassPathXmlApplicationContext("src/com/bjsxt/spring/factory/applicationContext.xml");
		BeanFactory f = new JDomTest("src/com/bjsxt/spring/factory/applicationContext.xml");

		Object o = f.getBean("v");
		Moveable m = (Moveable) o;
		m.run();
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值