Java中将xml转为List<实体类>

        我是一个IT新人,刚进入公司遇到了将Xml转为List<实体类>的情况,网上搜了一些资料,最后终于搞定,在这里分享一下

第一步:给出一个实体类C47

public class C47 {
    private String code;
    private String message;
    private String rownum;
    private String jylsh;       //检验流水号    varchar2
    private String jyjgbh;       //    检验机构编号    varchar2
    private String hphm;       //    号牌号码    varchar2
    private String hpzl;       //    号牌种类    varchar2
    private String wgjyzp;       //    外观检验照片    varchar2
    private String zlzp;       //    资料照片    varchar2
    private String wgjcxm;       //    外观检验项目    varchar2
    private String dtdpjyxm;       //    底盘动态检验项目    varchar2
    private String dpjyxm;       //    底盘检验项目    varchar2
    private String pdyj;       //    判定依据    char
    private String yqsbjyxm;       //    仪器设备检验项目    varchar2

}

第二步:将Xml转换为map放到List容器里面

 public static List<Map<String, Object>> xmlToMap(String result) throws IOException, SAXException, ParserConfigurationException {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        Document doc = db.parse(new InputSource(new ByteArrayInputStream(result.getBytes("GBK"))));
        NodeList bookList = doc.getElementsByTagName("vehispara");

        List<Map<String, Object>> list = new ArrayList();

        for (int i = 0; i < bookList.getLength(); i++) {
            Node book = bookList.item(i);
            NamedNodeMap attrs = book.getAttributes();
            for (int j = 0; j < attrs.getLength(); j++) {
                Node attr = attrs.item(j);
            }
            NodeList childNodes = book.getChildNodes();
            Map<String, Object> map = new HashMap();
            for (int k = 0; k < childNodes.getLength(); k++) {

                String Code = doc.getElementsByTagName("code").item(0).getChildNodes().item(0).getNodeValue();
                String Message = doc.getElementsByTagName("message").item(0).getChildNodes().item(0).getNodeValue();
                String Rownum = doc.getElementsByTagName("rownum").item(0).getChildNodes().item(0).getNodeValue();

                map.put("code", Code);
                map.put("message", Message);
                map.put("rownum", Rownum);

                //组织成某种格式 传回去然后 再转成entity
                if (childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) {
                    String name = childNodes.item(k).getNodeName();
                    String value = childNodes.item(k).getTextContent();
                    //set key,value
                    map.put(name, value);
                }
            }
            list.add(map);
        }
        return list;
    }

第三步:将第二步中返回的map转换为实体类

    public static Object transMap2Bean(Map<String, Object> map, Object obj) {
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                if (map.containsKey(key)) {
                    Object value = map.get(key);
                    // 得到property对应的setter方法
                    Method setter = property.getWriteMethod();
                    setter.invoke(obj, value);
                }
            }
        } catch (Exception e) {
        }
        return obj;
    }

 第三步: 遍历第一步返回的List,转换为实体类存放到List里面

 public static List<Object> getBeans(List<Map<String, Object>> list,Class<?> clazz) {
        Object obj1 = null;
        List<Object> list1 = new ArrayList<Object>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> stringObjectMap = list.get(i);
            Object obj =null;
            try {
                obj=clazz.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            obj1 = transMap2Bean(stringObjectMap, obj);
            list1.add(obj1);
        }
        return list1;
    }

方法已经完成,接下来我们测试一下哦

public static void main(String[] args) throws Exception {
//给出的Xml的String型
        String s = "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
                "<root>\n" +
                "<head>\n" +
                "<code>1</code>\n" +
                "<message>数据下载成功!</message>\n" +
                "<rownum>1</rownum>\n" +
                "</head>\n" +
                "<body>\n" +
                "<vehispara id=\"0\">\n" +
                "  <dtdpjyxm></dtdpjyxm>\n" +
                "  <pdyj>2</pdyj>\n" +
                "  <zlzp>0201,0202,0203,0204,0205,0206,0211,0209,0212,0213</zlzp>\n" +
                "  <yqsbjyxm>B1,B2,H1,H2,H3,H4</yqsbjyxm>\n" +
                "  <cyxm></cyxm>\n" +
                "  <cyzp>0111,0112,0113,0119,0115,0171,0174</cyzp>\n" +
                "  <hphm></hphm>\n" +
                "  <dpjyxm></dpjyxm>\n" +
                "  <jyjgbh>3703000017</jyjgbh>\n" +
                "  <jylsh>我是1</jylsh>\n" +
                "  <wgjyzp>0111,0112,0113,0119,0115,0171,0174</wgjyzp>\n" +
                "  <hpzl>02</hpzl>\n" +
                "  <wgjcxm>01,02,03,04,05,09,16,17,18,19,20,21,22,23,80</wgjcxm>\n" +
                "</vehispara>\n" +

                "<vehispara id=\"1\">\n" +
                "  <dtdpjyxm></dtdpjyxm>\n" +
                "  <pdyj>2</pdyj>\n" +
                "  <zlzp>0201,0202,0203,0204,0205,0206,0211,0209,0212,0213</zlzp>\n" +
                "  <yqsbjyxm>B1,B2,H1,H2,H3,H4</yqsbjyxm>\n" +
                "  <cyxm></cyxm>\n" +
                "  <cyzp>0111,0112,0113,0119,0115,0171,0174</cyzp>\n" +
                "  <hphm>鲁CZ513M</hphm>\n" +
                "  <dpjyxm></dpjyxm>\n" +
                "  <jyjgbh>3703000017</jyjgbh>\n" +
                "  <jylsh>你是2</jylsh>\n" +
                "  <wgjyzp>0111,0112,0113,0119,0115,0171,0174</wgjyzp>\n" +
                "  <hpzl>02</hpzl>\n" +
                "  <wgjcxm>01,02,03,04,05,09,16,17,18,19,20,21,22,23,80</wgjcxm>\n" +
                "</vehispara>\n" +
                "</body>\n" +
                "</root>";

        //TreeUtil是上面三个方法的实体类
        List<Map<String, Object>> list = TreeUtil.xmlToMap(s);
        //C47是我们要转换成的实体类
        List<Object> beans = TreeUtil.getBeans(TreeUtil.xmlToMap(s),C47.class);
        //我们遍历看一下结果哦
        for (int i = 0; i < beans.size(); i++) {
            System.out.println("结果是" + i + "    " + beans.get(i));
        }

是不是很期待结果哦

______________________________

结果是0    C47{code='1', message='数据下载成功!', rownum='1', jylsh='我是1', jyjgbh='3703000017', hphm='', hpzl='02', wgjyzp='0111,0112,0113,0119,0115,0171,0174', zlzp='0201,0202,0203,0204,0205,0206,0211,0209,0212,0213', wgjcxm='01,02,03,04,05,09,16,17,18,19,20,21,22,23,80', dtdpjyxm='', dpjyxm='', pdyj='2', yqsbjyxm='B1,B2,H1,H2,H3,H4'}
结果是1    C47{code='1', message='数据下载成功!', rownum='1', jylsh='你是2', jyjgbh='3703000017', hphm='鲁CZ513M', hpzl='02', wgjyzp='0111,0112,0113,0119,0115,0171,0174', zlzp='0201,0202,0203,0204,0205,0206,0211,0209,0212,0213', wgjcxm='01,02,03,04,05,09,16,17,18,19,20,21,22,23,80', dtdpjyxm='', dpjyxm='', pdyj='2', yqsbjyxm='B1,B2,H1,H2,H3,H4'}

 _______________________________

结果正如我们期待的,但是有一个局限性就是:返回的实体类的字段属性只是String类型,大家使用的时候一定要注意哦!

 

有没有好的解决方法,求帮忙!!!!!!!

 

关于上面的局限性问题,我已经解决, 大家可以参考这个链接  :  https://www.cnblogs.com/514929hgy/p/9143182.html

 

希望可以帮助大家!!!!!!

 

posted @ 2018-06-02 10:55  狮子无敌514  阅读(12073)  评论(0编辑  收藏  举报