SAX 解析XML 将xml转换成javaBean

来源:互联网 发布:蔡幸娟 星星知我心 编辑:程序博客网 时间:2024/05/27 14:14

SAX没有提供,直接将xml转成自定义javaBean的方法。

自己写了一个工具类通过反射机制转换。可以支持多数组。

/** * Created by tiantao on 16-7-20. */public class SaxUtil {    private static final Logger logger = LoggerFactory.getLogger(SaxUtil.class);    public static RequestBean converyRequestBean(String xml) {        RequestBean requestBean = new RequestBean();        try {            SAXBuilder sb = new SAXBuilder();            StringReader read = new StringReader(xml);            InputSource source = new InputSource(read);            Document doc = sb.build(source);            Element root = doc.getRootElement();            recursionConvery(root, RequestBean.class, requestBean);        } catch (Exception e) {            logger.error(e.toString(),e);            return null;        }        return requestBean;    }    private static void recursionConvery(Element e, Class<?> clazz, Object o) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {        List<Element> el = e.getChildren();        if (el.size() > 0) {            for (Element element : el) {                String childClassName = element.getName();                String getMethodName = "get" + childClassName.substring(0, 1).toUpperCase() + childClassName.substring(1);                String setMethodName = "set" + childClassName.substring(0, 1).toUpperCase() + childClassName.substring(1);                //当属性名为Class或class时,特殊处理get方法写成getclass                if ("getClass".equals(getMethodName)) {                    getMethodName = "getclass";                }                Class<?> clazz1 = null;                Method[] ms = clazz.getMethods();                for (Method m : ms) {                    if (getMethodName.equals(m.getName())) {                        clazz1 = m.getReturnType();                        if (clazz1.isArray()) {                            int arraySize = 0;                            for (Element element1 : el) {                                if (element1.getName().equals(clazz1.getComponentType().getSimpleName())) {                                    arraySize++;                                }                            }                            Object arrayObjects = Array.newInstance(clazz1.getComponentType(), arraySize);                            int n = 0;                            for (Element element1 : el) {                                if (element1.getName().equals(clazz1.getComponentType().getSimpleName())) {                                    Object oneO = clazz1.getComponentType().newInstance();                                    recursionConvery(element1, clazz1.getComponentType(), oneO);                                    Array.set(arrayObjects, n, oneO);                                    n++;                                }                            }                            Method setMethod = clazz.getMethod(setMethodName, clazz1);                            setMethod.invoke(o, arrayObjects);                        } else {                            if (element.getChildren() != null && element.getChildren().size() > 0) {                                Object o1 = clazz1.newInstance();                                recursionConvery(element, clazz1, o1);                                Method setMethod = clazz.getMethod(setMethodName, clazz1);                                setMethod.invoke(o, o1);                            } else {                                Method setMethod = clazz.getMethod(setMethodName, clazz1);                                setValue(element, setMethod, o);                            }                        }                    }                }            }        }    }    private static void setValue(Element element, Method method, Object o) throws InvocationTargetException, IllegalAccessException {        Class<?>[] c = method.getParameterTypes();        Object v = null;        if (element.getContentSize() > 0){            String text = element.getTextTrim();            String cn = c[0].getSimpleName();            if ("int".equals(cn) || "Integer".equals(cn)) {                v = Integer.parseInt(text);            } else if ("long".equals(cn) || "Long".equals(cn)) {                v = Long.parseLong(text);            } else if ("double".equals(cn) || "Double".equals(cn)) {                v = Double.parseDouble(text);            } else if ("float".equals(cn) || "Float".equals(cn)) {                v = Float.parseFloat(text);            } else {                v = text;            }        }        method.invoke(o, v);    }}


代码需要一些其他类,不能直接运行。迭代方法和设置属性值都是可以用的。

1 0
原创粉丝点击