首页

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

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

一、示例简介

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package xwood.mapping;
 
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
 
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
public class XmlCfgObjMappingTest {
      
    @SuppressWarnings("unused")
    private static Document loadXmlDoc() throws Exception{
        InputStream in = XmlCfgObjMappingTest.class.getResourceAsStream("/xwood/mapping/news_cfg.xml");
        SAXReader reader = new SAXReader();
        return reader.read(in);
    }
     
    @SuppressWarnings("unchecked")
    private static News setNewsObj(Element newNode) throws Exception{
        Class<News> new_cls = News.class;
        News new_obj = new_cls.newInstance();
        //获取new元素属性节点
        List<Element> n_new_properties = newNode.elements();
        for(Element p : n_new_properties){
            String p_name = p.getName();
            //获取news所有属性
            Field field = new_cls.getDeclaredField(p_name);
            //开放private protected访问权限
            field.setAccessible(true);
            Class<?> type = field.getType();
            Object p_value = p.getTextTrim();
            if(type==int.class){
                p_value = Integer.parseInt(p_value.toString());
            }else if(type==boolean.class){
                p_value = Boolean.parseBoolean(p_value.toString());
            }
            field.set(new_obj, p_value);// 动态设置值
        }
        return new_obj;
    }
     
     
    @SuppressWarnings("unchecked")
    public static  List<News>  getNews() throws Exception{
        //加载配置xml对象mappin文件
        Document doc=loadXmlDoc();
        //获取根节点,并找到new元素节点
        List<Element> n_news = doc.getRootElement().elements("new");
        List<News> newlist=new ArrayList<News>();
        for(Element n_new : n_news){
            newlist.add(setNewsObj(n_new));
        }
        return newlist;
    }
 
    public static void main(String[] args) throws Exception {
        List<News> news=getNews();
        for(News n:news){
            System.out.println(n.toString());
        }
    }
 
}

News类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package xwood.mapping;
 
public class News {
     
    private String title;
    private String location;
    private String kwords;
     
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
    public String getKwords() {
        return kwords;
    }
    public void setKwords(String kwords) {
        this.kwords = kwords;
    }
    @Override
    public String toString() {
        return super.toString()+"@title:"+title+"@location:"+location+"@kwords:"+kwords;
    }
 
}

news_cfg.xml配置文件

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

二、控制台结果

1
2
xwood.mapping.News@3a56f631@title:Java中遍历访问类几种常用方式的实现工具类ClassUtil@location:http://www.xwood.net/_site_domain_/_root/5870/5874/t_c262686.html@kwords:java、知识库
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
<<热门下载>>