xml与java双向映射

java类:

@Entity
@Table(name = "sc_default_score_range")
public class DefaultscoreRange implements java.io.Serializable
{
private Integer id;
@Attribute
private double score;
@Attribute
private String scoreRange;

public DefaultscoreRange()
{
}

public DefaultscoreRange(double score,String scoreRange)
{
this.score = score;
this.scoreRange = scoreRange;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id",unique = true,nullable = false)
public Integer getId()
{
return this.id;
}

public void setId(Integer id)
{
this.id = id;
}

@Column(name = "score",nullable = false,precision = 10)
public double getscore()
{
return this.score;
}

public void setscore(double score)
{
this.score = score;
}

@Column(name = "score_range",length = 200)
public String getscoreRange()
{
return this.scoreRange;
}

public void setscoreRange(String scoreRange)
{
this.scoreRange = scoreRange;
}

}

xml代码

<?xml version="1.0" encoding="utf-8"?>
<defaultscoreRangeList>

<defaultscoreRange score="150" scoreRange="35,54,72,90"/>
<defaultscoreRange score="100" scoreRange="24,36,48"/>
<defaultscoreRange score="90" scoreRange="22,32,43"/>
<defaultscoreRange score="60" scoreRange="15,22,29"/>

</defaultscoreRangeList>

xml与java映射类:

@Root
public class DefaultscoreRangeList
{
@ElementList(inline=true)
private List<DefaultscoreRange> defaultscoreRangeList = new ArrayList<DefaultscoreRange>();

public List<DefaultscoreRange> getDefaultscoreRangeList()
{
return defaultscoreRangeList;
}


public void setDefaultscoreRangeList(
List<DefaultscoreRange> defaultscoreRangeList)
{
this.defaultscoreRangeList = defaultscoreRangeList;
}
}

实现映射:

public static <ROOT> ROOT getXMLRoot(Class<ROOT> rootClass,String xmlclasspath){
Serializer serializer = new Persister();
ROOT root = null;
try {
root = serializer.read(rootClass,ResourceUtil.getResourceAsStream(xmlclasspath));
} catch (Exception e) {
throw new RuntimeException(e);
}
return root;
}

protected List<DefaultscoreRange> parse() { DefaultscoreRangeList rangeList = XMLUtil.getXMLRoot(DefaultscoreRangeList.class,"init/scDefaultscoreRange.xml"); return rangeList.getDefaultscoreRangeList(); }

相关文章

引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满足人的生产生活需要而产生的。具体...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
http://blog.jobbole.com/79252/ 引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满...
(点击上方公众号,可快速关注) 公众号:smart_android 作者:耿广龙|loonggg 点击“阅读原文”,可查看...
一、xml与xslt 相信所有人对xml都不陌生,其被广泛的应用于数据数据传输、保存与序列化中,是一种极为强...