In java, how does one use JDOM to parse xml files? The following small series will use examples for a detailed introduction. For more information, see
JDOM is an open-source project that uses the pure JAVA technology to parse, generate, serialize, and operate XML documents based on the tree structure. JDOM directly serves JAVA programming. It effectively integrates the functions of SAX and DOM by taking advantage of the many features of the more powerful JAVA language (method overloading, set concepts, and ing.
JDOM official address: http://www.jdom.org/
1. Create an interface and two classes to prepare for the future.
[Moveable. java]
Copy codeThe Code is as follows:
Package com. njupt. zhb. test;
Public interface Moveable {
Void run ();
}
[Plane. java]
Copy codeThe Code is as follows:
Package com. njupt. zhb. test;
Public class Plane implements Moveable {
@ Override
Public void run (){
// TODO Auto-generated method stub
System. out. println ("flying .....");
}
}
[Train. java]
Copy codeThe Code is 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 an interface. The main program can call the getBean method to obtain the corresponding object.
Copy codeThe Code is as follows:
Package com. njupt. zhb. test;
Public interface BeanFactory {
Object getBean (String id );
}
3. the xml file to be parsed is as follows:
Copy codeThe Code is 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. resolves the main class of the file and implements the BeanFactory interface.
Copy codeThe Code is 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> (); // stores the resolved id and Object.
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"); // obtain all 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"); // obtain the value corresponding to the id
String clazz = bean. getAttributeValue ("class"); // obtain the value of the class.
Object o = Class. forName (clazz). newInstance (); // Java reflection mechanism to generate an Object based on the Class name
MapContainer. put (id, o); // save to map
System. out. println (id + "" + clazz );
}
}
@ Override
Public Object getBean (String id ){
Return mapContainer. get (id );
}
}
5. The main program TestMain is called.
Copy codeThe Code is 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"); // obtain the Object labeled as train.
Moveable m1 = (Moveable) obj1; // subclass of the interface call
M1.run ();
////----------------------
Object obj2 = f. getBean ("plane ");
Moveable m2 = (Moveable) obj2;
M2.run ();
}
}
Experiment results:
Copy codeThe Code is as follows:
2
Train com. njupt. zhb. test. Train
Plane com. njupt. zhb. test. Plane
The train is flying ....
The plane is flying .....