java 使用JDOM解析xml文件
程序员文章站
2023-12-16 11:16:58
jdom是一个开源项目,它基于树型结构,利用纯java的技术对xml文档实现解析、生成、序列化以及多种操作。jdom直接为java编程服务。它利用更为强有力的java语言的...
jdom是一个开源项目,它基于树型结构,利用纯java的技术对xml文档实现解析、生成、序列化以及多种操作。jdom直接为java编程服务。它利用更为强有力的java语言的诸多特性(方法重载、集合概念以及映射),把sax和dom的功能有效地结合起来。
jdom的官方地址:1.首先新建一个接口和2个类,为后续做准备
[moveable.java]
package com.njupt.zhb.test;
public interface moveable {
void run();
}
[plane.java]
package com.njupt.zhb.test;
public class plane implements moveable {
@override
public void run() {
// todo auto-generated method stub
system.out.println("飞机在翱翔.....");
}
}
[train.java]
package com.njupt.zhb.test;
public class train implements moveable{
@override
public void run() {
system.out.println("火车在飞奔....");
}
}
2.新建一个接口,主程序可以调用getbean方法,获得相应的对象。
package com.njupt.zhb.test;
public interface beanfactory {
object getbean(string id);
}
3.需要解析的xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<beans>
<bean
id="train"
class="com.njupt.zhb.test.train">
</bean>
<bean
id="plane"
class="com.njupt.zhb.test.plane">
</bean>
</beans>
4.解析文件的主类,实现了beanfactory接口。
package com.njupt.zhb.test;
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 org.jdom.xpath.xpath;
public class classpathxmlapplicationcontext implements beanfactory {
private map<string, object> mapcontainer = new hashmap<string, object>();//用于存放解析出来的id和对象
public classpathxmlapplicationcontext(string filename) throws exception {
saxbuilder sb = new saxbuilder();
document doc = sb.build(this.getclass().getclassloader()
.getresourceasstream(filename));
element root = doc.getrootelement();
list list = xpath.selectnodes(root, "/beans/bean");//获得此节点下的所有值
system.out.println(list.size());
for (int i = 0; i < list.size(); i++) {
element bean = (element) list.get(i);
string id = bean.getattributevalue("id");//获得id对应的值
string clazz = bean.getattributevalue("class");//获得class对应的值
object o = class.forname(clazz).newinstance();//java反射机制,根据类名生成对象
mapcontainer.put(id, o);//保存到map中
system.out.println(id + " " + clazz);
}
}
@override
public object getbean(string id) {
return mapcontainer.get(id);
}
}
5.主程序testmain调用。
package com.njupt.zhb.test;
public class testmain {
public static void main(string[] args) throws exception {
beanfactory f = new classpathxmlapplicationcontext(
"com/njupt/zhb/test/sample.xml");
object obj1 = f.getbean("train");//获得标签为train的对象
moveable m1 = (moveable)obj1;//接口调用子类
m1.run();
// //----------------------
object obj2 = f.getbean("plane");
moveable m2 = (moveable) obj2;
m2.run();
}
}
实验结果:
2
train com.njupt.zhb.test.train
plane com.njupt.zhb.test.plane
火车在飞奔....
飞机在翱翔.....
点击下载源代码
jdom的官方地址:1.首先新建一个接口和2个类,为后续做准备
[moveable.java]
复制代码 代码如下:
package com.njupt.zhb.test;
public interface moveable {
void run();
}
[plane.java]
复制代码 代码如下:
package com.njupt.zhb.test;
public class plane implements moveable {
@override
public void run() {
// todo auto-generated method stub
system.out.println("飞机在翱翔.....");
}
}
[train.java]
复制代码 代码如下:
package com.njupt.zhb.test;
public class train implements moveable{
@override
public void run() {
system.out.println("火车在飞奔....");
}
}
2.新建一个接口,主程序可以调用getbean方法,获得相应的对象。
复制代码 代码如下:
package com.njupt.zhb.test;
public interface beanfactory {
object getbean(string id);
}
3.需要解析的xml文件如下:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<beans>
<bean
id="train"
class="com.njupt.zhb.test.train">
</bean>
<bean
id="plane"
class="com.njupt.zhb.test.plane">
</bean>
</beans>
4.解析文件的主类,实现了beanfactory接口。
复制代码 代码如下:
package com.njupt.zhb.test;
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 org.jdom.xpath.xpath;
public class classpathxmlapplicationcontext implements beanfactory {
private map<string, object> mapcontainer = new hashmap<string, object>();//用于存放解析出来的id和对象
public classpathxmlapplicationcontext(string filename) throws exception {
saxbuilder sb = new saxbuilder();
document doc = sb.build(this.getclass().getclassloader()
.getresourceasstream(filename));
element root = doc.getrootelement();
list list = xpath.selectnodes(root, "/beans/bean");//获得此节点下的所有值
system.out.println(list.size());
for (int i = 0; i < list.size(); i++) {
element bean = (element) list.get(i);
string id = bean.getattributevalue("id");//获得id对应的值
string clazz = bean.getattributevalue("class");//获得class对应的值
object o = class.forname(clazz).newinstance();//java反射机制,根据类名生成对象
mapcontainer.put(id, o);//保存到map中
system.out.println(id + " " + clazz);
}
}
@override
public object getbean(string id) {
return mapcontainer.get(id);
}
}
5.主程序testmain调用。
复制代码 代码如下:
package com.njupt.zhb.test;
public class testmain {
public static void main(string[] args) throws exception {
beanfactory f = new classpathxmlapplicationcontext(
"com/njupt/zhb/test/sample.xml");
object obj1 = f.getbean("train");//获得标签为train的对象
moveable m1 = (moveable)obj1;//接口调用子类
m1.run();
// //----------------------
object obj2 = f.getbean("plane");
moveable m2 = (moveable) obj2;
m2.run();
}
}
实验结果:
复制代码 代码如下:
2
train com.njupt.zhb.test.train
plane com.njupt.zhb.test.plane
火车在飞奔....
飞机在翱翔.....
点击下载源代码