首页

通过xml配置数据映射mapping给java对象属性赋值用做系统初始化配置(源码示例)

标签:SAXReader,解析,xml,映射,mapping,示例,dom4j-1.6.1.jar     发布时间:2016-09-03   

一、示例简介

本代码示例主要通过将新闻对象的初始化映射为配置文件news_cfg.xml的属性配置,通过dom4j(dom4j-1.6.1.jar)完成xml的解读,具体代码如下

package xwood.mapping;@b@@b@import java.io.InputStream;@b@import java.lang.reflect.Field;@b@import java.util.ArrayList;@b@import java.util.List;@b@@b@import org.dom4j.Document;@b@import org.dom4j.Element;@b@import org.dom4j.io.SAXReader;@b@@b@public class XmlCfgObjMappingTest {@b@     @b@    @SuppressWarnings("unused")@b@    private static Document loadXmlDoc() throws Exception{@b@        InputStream in = XmlCfgObjMappingTest.class.getResourceAsStream("/xwood/mapping/news_cfg.xml");@b@        SAXReader reader = new SAXReader();@b@        return reader.read(in);@b@    }@b@    @b@    @SuppressWarnings("unchecked")@b@    private static News setNewsObj(Element newNode) throws Exception{@b@        Class<News> new_cls = News.class;@b@        News new_obj = new_cls.newInstance();@b@        //获取new元素属性节点@b@        List<Element> n_new_properties = newNode.elements();@b@        for(Element p : n_new_properties){@b@            String p_name = p.getName();@b@            //获取news所有属性@b@            Field field = new_cls.getDeclaredField(p_name);@b@            //开放private protected访问权限@b@            field.setAccessible(true);@b@            Class<?> type = field.getType();@b@            Object p_value = p.getTextTrim();@b@            if(type==int.class){@b@                p_value = Integer.parseInt(p_value.toString());@b@            }else if(type==boolean.class){@b@                p_value = Boolean.parseBoolean(p_value.toString());@b@            }@b@            field.set(new_obj, p_value);// 动态设置值@b@        }@b@        return new_obj;@b@    }@b@    @b@    @b@    @SuppressWarnings("unchecked")@b@    public static  List<News>  getNews() throws Exception{@b@        //加载配置xml对象mappin文件@b@        Document doc=loadXmlDoc();@b@        //获取根节点,并找到new元素节点@b@        List<Element> n_news = doc.getRootElement().elements("new");@b@        List<News> newlist=new ArrayList<News>();@b@        for(Element n_new : n_news){@b@            newlist.add(setNewsObj(n_new));@b@        }@b@        return newlist;@b@    }@b@@b@    public static void main(String[] args) throws Exception {@b@        List<News> news=getNews();@b@        for(News n:news){@b@            System.out.println(n.toString());@b@        }@b@    }@b@@b@}

News类

package xwood.mapping;@b@@b@public class News {@b@    @b@    private String title;@b@    private String location;@b@    private String kwords;@b@    @b@    public String getTitle() {@b@        return title;@b@    }@b@    public void setTitle(String title) {@b@        this.title = title;@b@    }@b@    public String getLocation() {@b@        return location;@b@    }@b@    public void setLocation(String location) {@b@        this.location = location;@b@    }@b@    public String getKwords() {@b@        return kwords;@b@    }@b@    public void setKwords(String kwords) {@b@        this.kwords = kwords;@b@    }@b@    @Override@b@    public String toString() {@b@        return super.toString()+"@title:"+title+"@location:"+location+"@kwords:"+kwords;@b@    }@b@@b@}

news_cfg.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>@b@<news>@b@    <new>@b@        <!-- 标题 -->@b@        <title>Java中遍历访问类几种常用方式的实现工具类ClassUtil</title>@b@        <!-- 网址 -->@b@        <location>http://www.xwood.net/_site_domain_/_root/5870/5874/t_c262686.html</location>@b@        <!-- 关键词-->@b@        <kwords>java、知识库</kwords>@b@    </new>@b@    <new>@b@        <title>通过定义JS窗口对象获取url地址location.search部分的参数值</title>@b@        <location>http://www.xwood.net/_site_domain_/_root/5870/5874/t_c262647.html</location>@b@        <kwords>javascript、getParameter</kwords>@b@    </new>@b@</news>

二、控制台结果

xwood.mapping.News@3a56f631@title:Java中遍历访问类几种常用方式的实现工具类ClassUtil@location:http://www.xwood.net/_site_domain_/_root/5870/5874/t_c262686.html@kwords:java、知识库@b@xwood.mapping.News@233d28e3@title:通过定义JS窗口对象获取url地址location.search部分的参数值@location:http://www.xwood.net/_site_domain_/_root/5870/5874/t_c262647.html@kwords:javascript、getParameter