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();
}
}
推荐阅读
-
java使用dom4j生成与解析xml文档的方法示例
-
Python通过DOM和SAX方式解析XML的应用实例分享
-
Java下3中XML解析 DOM方式、SAX方式和StAX方式
-
Java解析XML的四种方法详解
-
PHP使用DOM对XML解析处理操作示例
-
java对XML文件的解析、节点的增加、删除操作总结
-
java解析xml字符串如何解析(解析xml字符串的方式)
-
python 解析XML python模块xml.dom解析xml实例代码
-
python网络编程学习笔记(八):XML生成与解析(DOM、ElementTree)
-
java操作(DOM、SAX、JDOM、DOM4J)xml方式的四种比较与详解