자바 반사 체 제 를 이용 하여 xml 를 맞 춥 니 다.

dom4j-1.6.jar

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class XMLUtilTest {
     @Test
     public void testXML(){
         Bean bean = new Bean();
         bean.setUsrname("  ");
         bean.setAge("25");
         bean.setCerNo("360402");
         bean.setGender(" ");
         bean.setMobileno("1888888");
         bean.setExter("");
         String strXml = bean.makeXml();
         System.out.println(strXml);
     }

     @Test
    public void testToBean() throws Exception {
        Bean bean = new Bean();
        String xml = "
"
+ "
"
+ "
"
+ " 25
"
+ " 360402
"
+ " 1888888
"
+ "
"
+ ""; bean.toBean(xml); System.out.println(bean); /**Bean{usrname=' ', age='25', cerNo='360402', mobileno='1888888', gender=' ', exter='null'}*/ } } class Base{ public String makeXml(){ String result = ""; Class clazz = this.getClass(); Field[] fileds = clazz.getDeclaredFields();// Document document = DocumentHelper.createDocument(); Element rootElement = document.addElement("SENF_MSG"); for (Field filed : fileds){ String paramName = filed.getName(); String methodName = "get" + paramName.substring(0, 1).toUpperCase() + paramName.substring(1); try { // public Method getMethod = clazz.getMethod(methodName); String paramVal = getMethod.invoke(this).toString(); if(!paramVal.equals("")&&paramVal!=null){ Element empName = rootElement.addElement(paramName); empName.setText(paramVal); } } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } result = document.asXML(); return result; } /** * xml bean * @param xmlStr * @return * @throws Exception */ public Base toBean(String xmlStr) throws Exception{ Class clazz = this.getClass(); Field[] fileds = clazz.getDeclaredFields(); Document document = DocumentHelper.parseText(xmlStr); Element root = document.getRootElement(); List rootList = root.elements(); for (int i=0;i"set" + paramName.substring(0, 1).toUpperCase() + paramName.substring(1); Method setMethod = null; setMethod = clazz.getMethod(methodName,String.class); setMethod.invoke(this,paramVal); } return this; } } class Bean extends Base{ private String usrname; private String age; private String cerNo; private String mobileno; private String gender; private String exter; public String getUsrname() { return usrname; } public void setUsrname(String usrname) { this.usrname = usrname; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getCerNo() { return cerNo; } public void setCerNo(String cerNo) { this.cerNo = cerNo; } public String getMobileno() { return mobileno; } public void setMobileno(String mobileno) { this.mobileno = mobileno; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getExter() { return exter; } public void setExter(String exter) { this.exter = exter; } @Override public String toString() { return "Bean{" + "usrname='" + usrname + '\'' + ", age='" + age + '\'' + ", cerNo='" + cerNo + '\'' + ", mobileno='" + mobileno + '\'' + ", gender='" + gender + '\'' + ", exter='" + exter + '\'' + '}'; } }

실행 결과:

<SENF_MSG><usrname>  usrname><age>25age><cerNo>360402cerNo><mobileno>1888888mobileno><gender> gender>SENF_MSG>

좋은 웹페이지 즐겨찾기