package xml.string;
import java.util.Date;
@SuppressWarnings("serial")
public class ItemDom4j implements java.io.Serializable {
private String item02;
private String item03;
private String item04;
private String item05;
private String item06;
private String item07;
private String item08;
private String item09;
private String item10;
public ItemDom4j() {
}
//get和set方法省略
}
package xml.string;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
/**
* xml格式字符串转换成为特定的对象
* @author zhanght
*
*/
public class FromStringToObj {
@SuppressWarnings({ "unchecked", "rawtypes" })
private static List FromxmlStrToObj(String xmlString){
List itemObjs = new ArrayList();
Document doc = null;
try {
doc = DocumentHelper.parseText(xmlString); //将字符串转换成为dom对象
Element rootElement = doc.getRootElement();//获取根节点
Iterator iterator = rootElement.elementIterator("tbclbizinterfacestandard"); //获取根节点下面的子节点
Class itemdom4j = Class.forName("xml.string.ItemDom4j");//类名
Object obj = null;//对象
Method method = null;//方法
while(iterator.hasNext()){
Element elet = (Element) iterator.next(); //tbclbizinterfacestandard节点
List<Element> list = elet.elements(); //tbclbizinterfacestandard下的子节点
String fieldName = "";
obj = itemdom4j.newInstance();
for(int i=0;i<list.size();i++){
fieldName = list.get(i).getName();//节点名称
Field field = itemdom4j.getDeclaredField(fieldName); //属性名称
Object type = field.getGenericType();//属性类型
String filedValue = elet.element(fieldName).getTextTrim();//属性值
String methodName = "set"+fieldName.replace(fieldName.charAt(0)+"",(fieldName.charAt(0)+"").toUpperCase()); //方法名称
if("class java.lang.String".equals(type.toString())){
method = itemdom4j.getMethod(methodName,String.class);
method.invoke(obj, filedValue);
}else if("class java.lang.Double".equals(type.toString())){
method = itemdom4j.getMethod(methodName,Double.class);
method.invoke(obj, Double.valueOf(filedValue));
}else if("class java.lang.Integer".equals(type.toString())){
method = itemdom4j.getMethod(methodName,Integer.class);
method.invoke(obj, Integer.valueOf(filedValue));
}else if("class java.lang.Long".equals(type.toString())){
method = itemdom4j.getMethod(methodName,Long.class);
method.invoke(obj, Long.valueOf(filedValue));
}
}
itemObjs.add(obj);
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return itemObjs;
}
public static void main(String[] args) {
String xmlStr= "<standardlist>"+ "<tbclbizinterfacestandard>"+ "<item02>it02</item02><item03>it03</item03><item04>it04</item04><item05></item05>"+ "</tbclbizinterfacestandard>"+
"<tbclbizinterfacestandard>"+ "<item02>it21</item02><item03>it23</item03><item04>it24</item04><item05></item05>"+ "</tbclbizinterfacestandard>"+ "<tbclbizinterfacestandard>"+ "<item02>it31</item02><item03>it32</item03><item04>it33</item04><item05></item05>"+ "</tbclbizinterfacestandard>"+ "<tbclbizinterfacestandard>"+ "<item02>it41</item02><item03>it43</item03><item04>it44</item04><item05></item05>"+
"</tbclbizinterfacestandard>"+
"</standardlist>";
FromxmlStrToObj(xmlStr);
}
}
最近的项目中需要把xml格式的字符串转换成对象保存到List中,查看网上资料根据dom4j和反射完成.代码在附件中可以直接运行