Parse XML files using JDOM
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.
Official JDOM address: Http://www.jdom.org/
Reference: Ma soldiers design model factory Model
1. Create an interface and two classes to prepare for the future.
[Moveable. Java]
package com.njupt.zhb.test;public interface Moveable {void run();}
[Plane. Java]
Package COM. njupt. ZHB. test; public class plane implements moveable {@ overridepublic void run () {// todo auto-generated method stubsystem. out. println ("The plane is flying ..... ");}}
[Train. Java]
Package COM. njupt. ZHB. test; public class train implements moveable {@ overridepublic 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.
package com.njupt.zhb.test;public interface BeanFactory {Object getBean(String id);}
3. the XML file to be parsed 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.
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 parsed 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 the values of 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 string clazz = bean corresponding to the ID. getattributevalue ("class"); // obtain the value of the Class Object o = Class. forname (clazz ). newinstance (); // Java reflection mechanism, which generates the object mapcontainer based on the class name. put (ID, O); // save it to System in map. out. println (ID + "" + clazz) ;}@ overridepublic object getbean (string ID) {return mapcontainer. get (ID );}}
5. The main program testmain is called.
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 the object moveable M1 = (moveable) obj1 labeled as train; // call the subclass m1.run (); /// ---------------------- object obj2 = f. getbean ("plane"); moveable m2 = (moveable) obj2; m2.run ();}}
Experiment results:
2 train com. njupt. ZHB. Test. trainplane com. njupt. ZHB. Test. Plane: The train is flying... the plane is flying .....
Source code: http://download.csdn.net/detail/nuptboyzhb/5756633