Parse the XML document and store the data in the database

Source: Internet
Author: User

<! -- 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 ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.