利用反射将xml转换为List<Object>
在将xml转为实体类的时候,一直对于类型为Integer, date等的很难处理, 无法转换, 但是后来我发现通过反射可以解决这一问题, 下面和大家分享一下
首先: 给出一个实体类Glxt19K01
public class Glxt19K01 { private String jylsh; // 检验流水号 varchar2 private String jyjgbh; private String jcxdh; // 检测线代号 varchar2 private Integer jycs; // 检验次数 number private String hpzl; // 号牌种类 varchar2 private String hphm; // 号牌号码 varchar2 private String clsbdh; // 车辆识别代号 varchar2 private String jyxm; // 检验项目 varchar2 private String bxpzh; // 保险凭证号 varchar2 private BigDecimal bxje; // 保险金额 number private String bxgs; // 保险公司 varchar2 private Date sxrq; // 生效日期 date private Date zzrq; // 终止日期 date private Timestamp jyrq; private Integer pdazt; // get set方法 }
其次 : 具体的实现方法readXML(), 这三个参数大家可以在测试的时候可以看出来,往下看看 ↓↓↓↓↓↓↓↓↓↓
public static List<Object> readXML(String tr2, String s, Class<?> clazz) {
    List<Object> list = new ArrayList();//创建list集合
    try {
        //1.创建一个SAXBuilder的对象
        SAXReader reader = new SAXReader();
        org.dom4j.Document doc = reader.read(new InputSource(new ByteArrayInputStream(s.getBytes("GBK"))));//dom4j读取
        // 4.通过document对象获取xml文件的根节点
        org.dom4j.Element root = doc.getRootElement();//获得根节点
        // 5. 获取根节点下的子节点body
        org.dom4j.Element body = root.element("body");
        // 6. 获取根节点下的二级节点
        org.dom4j.Element foo;
        for (Iterator i = body.elementIterator(tr2); i.hasNext(); ) {//遍历t.getClass().getSimpleName()节点
            foo = (org.dom4j.Element) i.next();//下一个二级节点
            //实例化Object对象
            Object obj = clazz.newInstance();
            Field[] properties = obj.getClass().getDeclaredFields();//获得实例的属性
            //实例的get方法
            Method getmeth;
            //实例的set方法
            Method setmeth;
            //遍历实体类的属性
            for (int j = 0; j < properties.length; j++) {
                //实例的set方法
                if (properties[j].getName().equals("serialVersionUID")) {
                    continue;
                } else {
                    setmeth = obj.getClass().getMethod(
                            "set"
                                    + properties[j].getName().substring(0, 1).toUpperCase()
                                    + properties[j].getName().substring(1), properties[j].getType());
                    Object setStr = foo.elementText(properties[j].getName());
                    if (foo.elementText(properties[j].getName()) != null&&!"".equals(foo.elementText(properties[j].getName()))) {
                        if (properties[j].getType() == java.util.Date.class) {
                            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            Date sj = df.parse(foo.elementText(properties[j].getName()));
                            setStr = sj;
                        } else if (properties[j].getType() == java.lang.Integer.class) {
                            setStr = Integer.parseInt(foo.elementText(properties[j].getName()));
                        } else if (properties[j].getType() == java.lang.Character.class) {
                            setStr = foo.elementText(properties[j].getName()).charAt(0);
                        }else if (properties[j].getType() == java.lang.Double.class) {
                            setStr = Double.parseDouble(foo.elementText(properties[j].getName()));
                        }
                    } else {
                        setStr = null;
                    }
                    //将对应节点的值存入
                    setmeth.invoke(obj, setStr);
                }
            }
            list.add(obj);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return list;
}
最后: 我们就测试一下啊
String s="<?xml version=\"1.0\" encoding=\"GBK\"?><root><body><vehispara><jylsh>37030118032900001</jylsh><jcxdh>1</jcxdh><jycs>1</jycs><hpzl>02</hpzl><hphm>鲁CK0048</hphm><clsbdh>LVZX32K7XDC517343</clsbdh><jyxm>F1,C1,DC,B1,B2,B0,H1,H4</jyxm><bxpzh></bxpzh><bxje>0</bxje><bxgs></bxgs><sxrq>2017-08-03</sxrq><zzrq>2018-08-03</zzrq><pdazt>1</pdazt></vehispara></body></root>";
List<Object> vehispara = QueryResult.readXML("vehispara", s, Glxt19K01.class); List<Glxt19K01> list = new ArrayList<Glxt19K01>(); for (int i = 0; i < vehispara.size(); i++) {
//将List<Object>转为对应的实体类 list.add((Glxt19K01) vehispara.get(i)); } for (int a = 0; a < list.size(); a++) { System.out.println("_______________________" + list.get(a)); }
这样我们就实现了啊
                    
                
                
            
        
浙公网安备 33010602011771号