XML处理数据

前端之家收集整理的这篇文章主要介绍了XML处理数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

将request里面的数据转到一个formbean里

final RoamOpenAF af = (RoamOpenAF) XmlUtil.xml2Form(request,
RoamOpenAF.class);

public class RoamOpenAF extends BaseAF
public class BaseAF extends ValidatorForm


package com.maywide.oss.commons.utils;

import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; /** * 包含对xml格式数据进行转换的方法 * @author user * */ public class XmlUtil { private static Log log = LogFactory.getLog(XmlUtil.class); /** * 将request里包含的xml格式的数据部分转到一个list里面 * * @param request * @param clazz * @return * @throws Exception */ public static List xml2List(HttpServletRequest request,Class clazz) throws Exception { List<Object> list = new ArrayList<Object>(); try { SAXBuilder sb = new SAXBuilder(); Document reqdoc = sb.build(request.getInputStream()); Element rootElement = reqdoc.getRootElement(); // 得到根元素 Object object = null; List dataList = rootElement.getChildren("data"); if (dataList == null || dataList.isEmpty()) return list; Element example = (Element) dataList.get(0); List childExampleList = example.getChildren(); if (childExampleList == null || childExampleList.isEmpty()) return list; String[] names = new String[childExampleList.size()]; for (int i = 0; i < childExampleList.size(); i++) { Element element = (Element) childExampleList.get(i); if (element.getName() == null || element.getName().trim().equals("")) continue; names[i] = element.getName(); } for (Iterator it = dataList.iterator(); it.hasNext();) { Element childElement = (Element) it.next(); object = clazz.newInstance(); for (int i = 0; i < names.length; i++) { Element e = childElement.getChild(names[i]); XmlUtil.setValue(names[i],e.getText(),object); } list.add(object); } } catch (Exception e) { throw e; } return list; } /** * 将request里面的数据转到一个formbean里 * @param request * @param clazz * @return * @throws Exception */ public static Object xml2Form(HttpServletRequest request,Class clazz) throws Exception { Object object = null; try { SAXBuilder sb = new SAXBuilder(); Document reqdoc = sb.build(request.getInputStream()); Element root = reqdoc.getRootElement(); List list = root.getChildren(); object = clazz.newInstance(); for (int i = 0; i < list.size(); i++) { Element e = (Element) list.get(i); String name = e.getName(); String value = e.getText(); if (name == null || name.trim().equals("")) continue; if (name.indexOf(".") >= 0) { String[] subNames = PubUtil.split(name,"."); if (subNames == null) continue; Object subObject = object; for (int j = 0; j < subNames.length; j++) { if (subNames[j] == null || subNames[j].trim().equals("")) continue; try { if (j < subNames.length - 1) { String methodName = "get" + subNames[j].substring(0,1) .toUpperCase() + subNames[j].substring(1); Method getMethod = subObject.getClass() .getDeclaredMethod(methodName,new Class[] {}); Object tmp = subObject; subObject = getMethod.invoke(subObject,new Object[] {}); Class type = getMethod.getReturnType(); if (subObject == null) { subObject = type.newInstance(); String setMethodName = "set" + subNames[j].substring(0,1) .toUpperCase() + subNames[j].substring(1); Method setMethod = tmp.getClass() .getDeclaredMethod( setMethodName,new Class[] { type .getClass() }); setMethod.invoke(tmp,subObject); } } else { XmlUtil.setValue(subNames[j],value,subObject); } } catch (NoSuchMethodException nsme) { continue; } } } else { XmlUtil.setValue(name,object); } } } catch (Exception e) { throw e; } return object; } /** * 把object里名为field的属性的值设为value * @param field * 要设置的属性 * @param value * 要设置的值 * @param object * 被设置的对象 * @throws Exception */ public static void setValue(String field,String value,Object object) throws Exception { if (field == null || field.trim().equals("")) return; String methodName = "set" + field.substring(0,1).toUpperCase() + field.substring(1); Method[] methods = object.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { if (methodName.equals(methods[i].getName())) { Class[] paramClass = methods[i].getParameterTypes(); if (paramClass == null || paramClass.length != 1) continue; if (paramClass[0].equals(String.class)) { methods[i].invoke(object,new Object[] { value }); } else if (paramClass[0].equals(Long.class)) { if (value == null || value.equals("")) { Long tempLong = null; methods[i].invoke(object,new Object[] { tempLong }); } else { methods[i].invoke(object,Long.valueOf(value)); } } else if (paramClass[0].equals(Double.class)) { if (value == null || value.equals("")) { Double tempDouble = null; methods[i].invoke(object,new Object[] { tempDouble }); } else { methods[i].invoke(object,Double.valueOf(value)); } } else if (paramClass[0].equals(java.util.Date.class)) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.US); Date date = null; try { if (!StringUtils.isEmpty(value)){ if (!value.contains(":")){ //因为formatter的格式为:yyyy-MM-dd HH:mm:ss,所以,value必须为包括"时分秒"。 //否则会出现unknown source,并throw parseException。 //此处,只做简单的判断 value = value + " 00:00:00"; } date = formatter.parse(value); } } catch(Exception ex) { log.error("parse date exception,case: ",ex); } methods[i].invoke(object,new Object[] { date }); } break; } } } }

猜你在找的XML相关文章