Jdom is an open source project that uses pure Java technology to parse, generate, serialize, and manipulate XML documents based on a tree-structured architecture. Jdom directly for Java programming services. It leverages the many features of the more powerful Java language (method overload, set concepts, and mappings) to effectively combine the functionality of Sax and DOM.
Jdom's official address: http://www.jdom.org/
1. First create a new interface and 2 classes to prepare for follow-up
[Moveable.java]
Copy Code code as follows:
Package com.njupt.zhb.test;
public interface Moveable {
void run ();
}
[Plane.java]
Copy Code code as follows:
Package com.njupt.zhb.test;
public class Plane implements moveable {
@Override
public void Run () {
TODO auto-generated Method Stub
System.out.println ("The plane is soaring ...");
}
}
[Train.java]
Copy Code code as follows:
Package com.njupt.zhb.test;
public class Train implements moveable{
@Override
public void Run () {
System.out.println ("The Train is flying ...");
}
}
2. Create a new interface, the main program can call the Getbean method, get the corresponding object.
Copy Code code as follows:
Package com.njupt.zhb.test;
Public interface Beanfactory {
Object Getbean (String ID);
}
3. The XML file that needs to be parsed is as follows:
Copy Code code as follows:
<?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. Parsing the main class of the file, implements the Beanfactory interface.
Copy Code code as follows:
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> ();//For storing parsed IDs and objects
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");//Get all the values under this node
System.out.println (List.size ());
for (int i = 0; i < list.size (); i++) {
Element bean = (Element) list.get (i);
String id = bean.getattributevalue ("id");//get ID corresponding value
String clazz = Bean.getattributevalue ("class");//get class corresponding value
Object o = Class.forName (clazz). newinstance ();//java reflection mechanism, generating objects based on class names
Mapcontainer.put (ID, O);//Save in Map
System.out.println (id + "" + clazz);
}
}
@Override
Public Object Getbean (String ID) {
return Mapcontainer.get (ID);
}
}
5. Main program Testmain call.
Copy Code code as follows:
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");//Get label as Train
Moveable M1 = (moveable) obj1;//interface invoke subclass
M1.run ();
// //----------------------
Object obj2 = F.getbean ("plane");
Moveable m2 = (moveable) obj2;
M2.run ();
}
}
Experimental Results:
Copy Code code as follows:
2
Train Com.njupt.zhb.test.Train
Plane Com.njupt.zhb.test.Plane
The train is flying ....
The plane is soaring ...
Click to download source code