欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java利用dom/jdom解析XML

程序员文章站 2022-05-30 10:00:01
...

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();
	}

}