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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<SENF_MSG>\n" +
" <usrname>李四</usrname>\n" +
" <age>25</age>\n" +
" <cerNo>360402</cerNo>\n" +
" <mobileno>1888888</mobileno>\n" +
" <gender>男</gender>\n" +
"</SENF_MSG>";
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("")&¶mVal!=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<Element> rootList = root.elements();
for (int i=0;i<rootList.size();i++){
Element temp = rootList.get(i);
String paramName = temp.getName();
String paramVal = temp.getText();
String methodName = "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 + '\'' +
'}';
}
}
运行结果:
<?xml version="1.0" encoding="UTF-8"?>
<SENF_MSG><usrname>李四</usrname><age>25</age><cerNo>360402</cerNo><mobileno>1888888</mobileno><gender>男</gender></SENF_MSG>