博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SAX 解析XML 将xml转换成javaBean
阅读量:4215 次
发布时间:2019-05-26

本文共 4599 字,大约阅读时间需要 15 分钟。

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
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); }}

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

转载地址:http://omnmi.baihongyu.com/

你可能感兴趣的文章
S3C2440上touchscreen触摸屏驱动
查看>>
USB History Viewing
查看>>
怎样做可靠的分布式锁,Redlock 真的可行么?
查看>>
[图文] Seata AT 模式分布式事务源码分析
查看>>
pm 源码分析
查看>>
Sending the User to Another App
查看>>
kmsg_dump
查看>>
Getting a Result from an Activity
查看>>
Allowing Other Apps to Start Your Activity
查看>>
dev/mem
查看>>
pfn_valid 源码分析
查看>>
dev/kmem 和dev/mem的区别
查看>>
checkbox
查看>>
Sending Simple Data to Other Apps
查看>>
Receiving Simple Data from Other Apps
查看>>
中断API之__tasklet_schedule
查看>>
中断API之enable_irq
查看>>
中断API之disable_irq
查看>>
nova 中的guestfs
查看>>
nova中的localfs
查看>>