<! -- Organization Management -->
<Entity class = "Company"
Name = ""
Description = "seed pearl"
Call = "partyService. addParty">
<Entity class = "Department" name = "Academic Affairs Office">
<Entity class = "Position" name = "">
<Entity class = "Person" name = "Zhang San" sex = "male" tel = "1111">
</Entity>
</Entity>
<Entity class = "Position" name = "">
<Entity class = "Person" name = "" sex = "female" tel = "1222">
</Entity>
</Entity>
</Entity>
<Entity class = "Department" name = "library">
<Entity class = "Position" name = "">
<Entity class = "Person" name = "Wang Wu" sex = "female" tel = "2222">
</Entity>
<Entity class = "Person" name = "Zhao "sex = "male" tel = "2111">
</Entity>
</Entity>
</Entity>
<Entity class = "Company" name = "Emy-South Campus"> </entity>
</Entity>
Try {
// Parse the init. xml document
Document doc = new SAXReader (). read (Thread. currentThread (). getContextClassLoader (). getResourceAsStream (init. xml ));
// Obtain the root element
Element root = doc. getRootElement ();
// Get the package name
String pkg = root. valueOf ("@ package ");
// Obtain the entity set under the root element
List <Element> entities = root. selectNodes ("entity ");
For (Iterator <Element> iter = entities. iterator (); iter. hasNext ();){
Element e = iter. next ();
AddEntity (e, pkg, null, null );
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
Private void addEntity (Element e, String pkg, Object parent, String callString ){
Try {
// Process the current Element
// 1. What type of object to create
// The full-outsourcing name of the class to be created
String className = pkg + "." + e. attributeValue ("class ");
// Create an object based on the class name
Object entity = Class. forName (className). newInstance ();
// Assign values to attributes of an object
Iterator iter = e. attributeIterator ();
While (iter. hasNext ()){
Attribute attr = (Attribute) iter. next ();
String propName = attr. getName ();
// Judge the assignment of other attributes except the class and call attributes
If (! "Class". equals (propName )&&! "Call". equals (propName )){
String propValue = attr. getValue ();
BeanUtils. copyProperty (entity, propName, propValue );
}
}
// Assign values to the attributes of the entity parent object
BeanUtils. copyProperty (entity, "parent", parent );
// 2. Storage object (which method of which Service is called ?)
String call = e. attributeValue ("call ");
If (call! = Null ){
CallString = call;
}
If (callString = null ){
Throw new RuntimeException ("unable to create object, call Method unknown! ");
}
// 3. Call the corresponding method to store the object
String [] mesg = callString. split ("\\.");
String serviceName = mesg [0];
String methodName = mesg [1];
// Obtain the Service object
Object serviceObject = factory. getBean (serviceName );
// Obtain the reflection class of the method on the Servce object to be called
For (Method m: serviceObject. getClass (). getMethods ()){
If (methodName. equals (m. getName ())){
// Call this method
M. invoke (serviceObject, entity );
}
}
// 4. check whether there are child elements in the current Element.
List <Element> subEntities = e. elements ("entity ");
For (Iterator <Element> itr = subEntities. iterator (); itr. hasNext ();){
Element subElement = itr. next ();
AddEntity (subElement, pkg, entity, callString );
}
} Catch (Exception e1 ){
E1.printStackTrace ();
}
}